1#!/bin/sh
2# libtool-reloc - libtool wrapper with support for relocatable programs
3# Copyright (C) 2019-2020 Free Software Foundation, Inc.
4# Written by Bruno Haible <bruno@clisp.org>, 2019.
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19# Usage: libtool-reloc libtool LIBTOOL_ARGUMENTS
20
21# Outputs a command and runs it.
22func_verbose ()
23{
24  # Make it easy to copy&paste the printed command into a shell in most cases,
25  # by escaping '\\', '"', and '$'. This is not perfect, just good enough.
26  echo "$@" | sed -e 's/\([\\"$]\)/\\\1/g'
27  "$@"
28}
29
30# Determine the mode from the arguments.
31mode=
32for arg
33do
34  case "$arg" in
35    --mode=link) mode=link ;;
36  esac
37done
38
39if test "$mode" = link; then
40  # Determine the target from the arguments.
41  target=
42  next_is_target=false
43  for arg
44  do
45    if $next_is_target; then
46      target="$arg"
47      next_is_target=false
48    else
49      case "$arg" in
50        -o) next_is_target=true ;;
51        *) next_is_target=false ;;
52      esac
53    fi
54  done
55  case "$target" in
56    *.la)
57      # When creating a library:
58      # 1. Add a '-Wl,-rpath,@loader_path' option.
59      #    (A '-R @loader_path' option does not work: libtool produces
60      #    an error "error: only absolute run-paths are allowed".)
61      #    (Also note that 'install_name_tool -add_rpath @loader_path ...'
62      #    does not work on Mac OS X 10.5.)
63      #    This is done through the RELOCATABLE_LDFLAGS macro.
64      # 2. After creating the library, run
65      #    install_name_tool -id @rpath/$dlname $target_dir/.libs/$dlname
66      #    (This is easier than to modify the libtool script to emit a different
67      #    install_name. Also, an option '-Wl,-install_name,@rpath/$dlname' does
68      #    not work since libtool emits another option '-Wl,-install_name,...'
69      #    after it.
70      "$@" && {
71        dlname_assignment=`grep '^dlname=' "$target"`
72        dlname=
73        eval "$dlname_assignment"
74        # Nothing to do when --disable-shared was specified.
75        if test -n "$dlname"; then
76          target_dir=`dirname "$target"`
77          if test -f "$target_dir/.libs/$dlname"; then
78            func_verbose install_name_tool -id "@rpath/$dlname" "$target_dir/.libs/$dlname"
79          fi
80        fi
81      }
82      ;;
83    *)
84      "$@"
85      ;;
86  esac
87else
88  "$@"
89fi
90