Re: relocatable-prog: Use $ORIGIN trick on more platforms

2019-03-04 Thread Bruno Haible
> 2019-02-23  Bruno Haible  
> 
>   relocatable-prog: Use wrapper-free installation also on Mac OS X.
>   Reported by Paul Smith .
>   * build-aux/install-reloc: Accept a 'mode' argument as first argument.
>   (func_relativize): New function, from gnulib-tool.
>   Handle mode 'macosx' through invocations of 'otool' and
>   'install_name_tool'.
>   * m4/relocatable.m4 (gl_RELOCATABLE_BODY): Determine use_macos_tools.
>   If use_macos_tools is true, set INSTALL_PROGRAM_ENV to an
>   'install-reloc' invocation with mode 'macosx'.

This patch works for executables that reference shared libraries
in the same package. But it does not work for shared libraries that
reference other shared libraries in the same package. I'm therefore
reverting this patch and adding this one instead.


2019-03-04  Bruno Haible  

relocatable-prog: Use wrapper-free installation on Mac OS X, take 2.
This approach supports relocatable installation of shared libraries
which depend on other shared libraries from the same package.
* m4/relocatable.m4 (gl_RELOCATABLE_BODY): Determine use_macos_tools.
If use_macos_tools is true, use reloc-ldflags and set LIBTOOL to be a
wrapper around the original LIBTOOL.
* build-aux/reloc-ldflags: Add support for Mac OS X, which uses the
token '@loader_path' instead of '$ORIGIN'.
* build-aux/libtool-reloc: New file.
* modules/relocatable-prog (Files): Add it.
* doc/relocatable-maint.texi (Supporting Relocation): Update to match
the recent changes. Document the need to set the *_LDFLAGS of libraries.
RELOCATABLE_LIBRARY_PATH and RELOCATABLE_CONFIG_H_DIR should be set in
Makefile.am, not in configure.ac.

diff --git a/build-aux/libtool-reloc b/build-aux/libtool-reloc
new file mode 100755
index 000..9d899ab
--- /dev/null
+++ b/build-aux/libtool-reloc
@@ -0,0 +1,89 @@
+#!/bin/sh
+# libtool-reloc - libtool wrapper with support for relocatable programs
+# Copyright (C) 2019 Free Software Foundation, Inc.
+# Written by Bruno Haible , 2019.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see .
+
+# Usage: libtool-reloc libtool LIBTOOL_ARGUMENTS
+
+# Outputs a command and runs it.
+func_verbose ()
+{
+  # Make it easy to copy the printed command into a shell in most cases,
+  # by escaping '\\', '"', and '$'. This is not perfect, just good enough.
+  echo "$@" | sed -e 's/\([\\"$]\)/\\\1/g'
+  "$@"
+}
+
+# Determine the mode from the arguments.
+mode=
+for arg
+do
+  case "$arg" in
+--mode=link) mode=link ;;
+  esac
+done
+
+if test "$mode" = link; then
+  # Determine the target from the arguments.
+  target=
+  next_is_target=false
+  for arg
+  do
+if $next_is_target; then
+  target="$arg"
+  next_is_target=false
+else
+  case "$arg" in
+-o) next_is_target=true ;;
+*) next_is_target=false ;;
+  esac
+fi
+  done
+  case "$target" in
+*.la)
+  # When creating a library:
+  # 1. Add a '-Wl,-rpath,@loader_path' option.
+  #(A '-R @loader_path' option does not work: libtool produces
+  #an error "error: only absolute run-paths are allowed".)
+  #(Also note that 'install_name_tool -add_rpath @loader_path ...'
+  #does not work on Mac OS X 10.5.)
+  #This is done through the RELOCATABLE_LDFLAGS macro.
+  # 2. After creating the library, run
+  #install_name_tool -id @rpath/$dlname $target_dir/.libs/$dlname
+  #(This is easier than to modify the libtool script to emit a 
different
+  #install_name. Also, an option '-Wl,-install_name,@rpath/$dlname' 
does
+  #not work since libtool emits another option '-Wl,-install_name,...'
+  #after it.
+  "$@" && {
+dlname_assignment=`grep '^dlname=' "$target"`
+dlname=
+eval "$dlname_assignment"
+# Nothing to do when --disable-shared was specified.
+if test -n "$dlname"; then
+  target_dir=`dirname "$target"`
+  if test -f "$target_dir/.libs/$dlname"; then
+func_verbose install_name_tool -id "@rpath/$dlname" 
"$target_dir/.libs/$dlname"
+  fi
+fi
+  }
+  ;;
+*)
+  "$@"
+  ;;
+  esac
+else
+  "$@"
+fi
diff --git a/build-aux/reloc-ldflags b/build-aux/reloc-ldflags
index 3aed330..625fef2 100755
--- 

Re: relocatable-prog: Use $ORIGIN trick on more platforms

2019-02-23 Thread Bruno Haible
And here's an update of the documentation:


2019-02-23  Bruno Haible  

relocatable-prog: Update documentation.
* doc/relocatable-maint.texi (Supporting Relocation): Update to match
the recent changes.

diff --git a/doc/relocatable-maint.texi b/doc/relocatable-maint.texi
index b95caaf..9f1b893 100644
--- a/doc/relocatable-maint.texi
+++ b/doc/relocatable-maint.texi
@@ -35,14 +35,21 @@ here in a platform-specific way:
 
 @itemize
 @item
-On GNU/Linux, it adds a linker option (@option{-rpath}) that causes
-the dynamic linker to search for libraries in a directory relative to
-the location of the invoked executable.
+On most operating systems, it adds a linker option (@option{-rpath}) that
+causes the dynamic linker to search for libraries in a directory relative
+to the location of the invoked executable.  This works on GNU/Linux and
+modern versions of FreeBSD, NetBSD, OpenBSD, Solaris, Haiku.
 
 @item
-On other Unix systems, it installs a wrapper executable.  The wrapper
+On macOS, it modifies the installed executables after installation in a way
+that causes the dynamic linker to search for libraries in a directory relative
+to the location of the invoked executable.
+
+@item
+On other Unix systems, it installs a trampoline executable.  The trampoline
 sets the environment variable that controls shared library searching
 (usually @env{LD_LIBRARY_PATH}) and then invokes the real executable.
+This applies to operating systems such as AIX, HP-UX, or Minix.
 
 @item
 On Windows, the executable's own directory is searched for libraries,




Re: relocatable-prog: Use $ORIGIN trick on more platforms

2019-02-23 Thread Bruno Haible
Recent enough Hurd versions support $ORIGIN as well. This patch
enables support for it in the 'relocatable-prog' module.


2019-02-23  Bruno Haible  

relocatable-prog: Use $ORIGIN trick also on GNU/Hurd.
* m4/relocatable.m4 (gl_RELOCATABLE_BODY): Use $ORIGIN trick also on
Hurd with glibc >= 2.27.

diff --git a/m4/relocatable.m4 b/m4/relocatable.m4
index 0044477..faa23f2 100644
--- a/m4/relocatable.m4
+++ b/m4/relocatable.m4
@@ -1,4 +1,4 @@
-# relocatable.m4 serial 20
+# relocatable.m4 serial 21
 dnl Copyright (C) 2003, 2005-2007, 2009-2019 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -40,16 +40,24 @@ AC_DEFUN([gl_RELOCATABLE_BODY],
 enable_rpath=no
 AC_CHECK_HEADERS([mach-o/dyld.h])
 AC_CHECK_FUNCS([_NSGetExecutablePath])
-changequote(,)dnl
 case "$host_os" in
   mingw*) is_noop=yes ;;
   # For the platforms that support $ORIGIN, see
   # .
   # glibc systems, Linux with musl libc: yes. Android: no.
-  # Hurd: no 
.
   linux*-android*) ;;
-  gnu*) ;;
   linux* | kfreebsd*) use_elf_origin_trick=yes ;;
+  # Hurd: 

+  # only after the glibc commit from 2018-01-08
+  # 

+  gnu*)
+# Test for a glibc version >= 2.27.
+AC_CHECK_FUNCS([copy_file_range])
+if test $ac_cv_func_copy_file_range = yes; then
+  use_elf_origin_trick=yes
+fi
+;;
+changequote(,)dnl
   # FreeBSD >= 7.3, DragonFly >= 3.0: yes.
   freebsd | freebsd[1-7] | freebsd[1-6].* | freebsd7.[0-2]) ;;
   dragonfly | dragonfly[1-2] | dragonfly[1-2].*) ;;
@@ -66,8 +74,8 @@ changequote(,)dnl
   solaris*) use_elf_origin_trick=yes ;;
   # Haiku: yes.
   haiku*) use_elf_origin_trick=yes ;;
-esac
 changequote([,])dnl
+esac
 if test $is_noop = yes; then
   RELOCATABLE_LDFLAGS=:
   AC_SUBST([RELOCATABLE_LDFLAGS])




Re: relocatable-prog: Use $ORIGIN trick on more platforms

2019-02-20 Thread Paul Smith
On Wed, 2019-02-20 at 02:45 +0100, Bruno Haible wrote:
> Paul Smith noted on gnu-prog-discuss that other systems than glibc
> have support for $ORIGIN in rpath. This patch changes the
> 'relocatable-prog' module to make use of this feature, thus removing
> the need for a "wrapper"/"trampoline" executable on these platforms.

MacOS has @executable_path which is the same as $ORIGIN.  See:

https://wincent.com/wiki/%40executable_path%2C_%40load_path_and_%40rpath

Unfortunately on MacOS they have "applications" which are much more
complicated and can have frameworks etc.  But I think @executable_path
is pretty close to exactly the same thing as $ORIGIN and works fine if
you're just building a normal application.




relocatable-prog: Use $ORIGIN trick on more platforms

2019-02-19 Thread Bruno Haible
Paul Smith noted on gnu-prog-discuss that other systems than glibc have support
for $ORIGIN in rpath. This patch changes the 'relocatable-prog' module to
make use of this feature, thus removing the need for a "wrapper"/"trampoline"
executable on these platforms.

Tested on
  - FreeBSD 11,
  - NetBSD 7, 8,
  - OpenBSD 6,
  - Solaris 9, 10, 11,
  - Haiku.


2019-02-19  Bruno Haible  

    relocatable-prog: Use $ORIGIN trick on more platforms.
* m4/relocatable.m4 (gl_RELOCATABLE_BODY): Use $ORIGIN trick also on
FreeBSD >= 7.3, DragonFly >= 3.0, NetBSD >= 8.0, OpenBSD >= 5.4,
Solaris >= 10, Haiku. But don't use it on Android.
* build-aux/reloc-ldflags: Allow the use of the $ORIGIN trick also on
Hurd, FreeBSD, DragonFly, NetBSD, OpenBSD, Solaris, Haiku.

diff --git a/m4/relocatable.m4 b/m4/relocatable.m4
index c55f7b4..0044477 100644
--- a/m4/relocatable.m4
+++ b/m4/relocatable.m4
@@ -1,4 +1,4 @@
-# relocatable.m4 serial 19
+# relocatable.m4 serial 20
 dnl Copyright (C) 2003, 2005-2007, 2009-2019 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -40,10 +40,34 @@ AC_DEFUN([gl_RELOCATABLE_BODY],
 enable_rpath=no
 AC_CHECK_HEADERS([mach-o/dyld.h])
 AC_CHECK_FUNCS([_NSGetExecutablePath])
+changequote(,)dnl
 case "$host_os" in
   mingw*) is_noop=yes ;;
+  # For the platforms that support $ORIGIN, see
+  # <https://lekensteyn.nl/rpath.html>.
+  # glibc systems, Linux with musl libc: yes. Android: no.
+  # Hurd: no 
<http://lists.gnu.org/archive/html/bug-hurd/2019-02/msg00049.html>.
+  linux*-android*) ;;
+  gnu*) ;;
   linux* | kfreebsd*) use_elf_origin_trick=yes ;;
+  # FreeBSD >= 7.3, DragonFly >= 3.0: yes.
+  freebsd | freebsd[1-7] | freebsd[1-6].* | freebsd7.[0-2]) ;;
+  dragonfly | dragonfly[1-2] | dragonfly[1-2].*) ;;
+  freebsd* | dragonfly*) use_elf_origin_trick=yes ;;
+  # NetBSD >= 8.0: yes.
+  netbsd | netbsd[1-7] | netbsd[1-7].*) ;;
+  netbsdelf | netbsdelf[1-7] | netbsdelf[1-7].*) ;;
+  netbsd*) use_elf_origin_trick=yes ;;
+  # OpenBSD >= 5.4: yes.
+  openbsd | openbsd[1-5] | openbsd[1-4].* | openbsd5.[0-3]) ;;
+  openbsd*) use_elf_origin_trick=yes ;;
+  # Solaris >= 10: yes.
+  solaris | solaris2.[1-9] | solaris2.[1-9].*) ;;
+  solaris*) use_elf_origin_trick=yes ;;
+  # Haiku: yes.
+  haiku*) use_elf_origin_trick=yes ;;
 esac
+changequote([,])dnl
 if test $is_noop = yes; then
   RELOCATABLE_LDFLAGS=:
   AC_SUBST([RELOCATABLE_LDFLAGS])
diff --git a/build-aux/reloc-ldflags b/build-aux/reloc-ldflags
index 4f2b10d..3aed330 100755
--- a/build-aux/reloc-ldflags
+++ b/build-aux/reloc-ldflags
@@ -54,7 +54,12 @@ case "$installdir" in
 esac
 
 case "$host_os" in
-  linux* | kfreebsd*)
+  linux* | gnu* | kfreebsd* | \
+  freebsd* | dragonfly* | \
+  netbsd* | \
+  openbsd* | \
+  solaris* | \
+  haiku*)
 rpath=
 save_IFS="$IFS"; IFS=":"
 for dir in $library_path_value; do
@@ -89,7 +94,14 @@ case "$host_os" in
 IFS="$save_IFS"
 # Output it.
 if test -n "$rpath"; then
-  echo "-Wl,-rpath,$rpath"
+  case "$host_os" in
+# At least some versions of FreeBSD, DragonFly, and OpenBSD need the
+# linker option "-z origin". See <https://lekensteyn.nl/rpath.html>.
+freebsd* | dragonfly* | openbsd*)
+  echo "-Wl,-z,origin -Wl,-rpath,$rpath" ;;
+*)
+  echo "-Wl,-rpath,$rpath" ;;
+  esac
 fi
 ;;
   *)