1#! /bin/sh
2# Output a system dependent linker command for putting a relocatable library
3# search path into an executable.
4#
5#   Copyright 2003 Free Software Foundation, Inc.
6#   Written by Bruno Haible <bruno@clisp.org>, 2003.
7#
8#   This program is free software; you can redistribute it and/or modify
9#   it under the terms of the GNU General Public License as published by
10#   the Free Software Foundation; either version 2 of the License, or
11#   (at your option) any later version.
12#
13#   This program is distributed in the hope that it will be useful, but
14#   WITHOUT ANY WARRANTY; without even the implied warranty of
15#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16#   General Public License for more details.
17#
18#   You should have received a copy of the GNU General Public License
19#   along with this program; if not, write to the Free Software Foundation,
20#   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21#
22#   As a special exception to the GNU General Public License, if you
23#   distribute this file as part of a program that contains a
24#   configuration script generated by Autoconf, you may include it under
25#   the same distribution terms that you use for the rest of that program.
26#
27# The first argument passed to this file is the canonical host specification,
28#    CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM
29# or
30#    CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM
31# The environment variable LD should be set by the caller.
32#
33# The second argument is a colon separated list of directories that contain
34# the libraries at installation time.
35#
36# The third argument is the directory into which the executable is going to be
37# installed.
38
39host="$1"
40host_cpu=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\1/'`
41host_vendor=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\2/'`
42host_os=`echo "$host" | sed 's/^\([^-]*\)-\([^-]*\)-\(.*\)$/\3/'`
43
44library_path_value=$2
45
46installdir=$3
47
48# Verify that installdir is absolute.
49case "$installdir" in
50  /*) ;;
51  *)
52    echo "installdir is not absolute: $installdir" 1>&2
53    exit 1
54    ;;
55esac
56
57case "$host_os" in
58  linux*) # Supported since Linux 2.1 and glibc 2.1.
59    rpath=
60    save_IFS="$IFS"; IFS=":"
61    for dir in $library_path_value; do
62      IFS="$save_IFS"
63      case "$dir" in
64        /*)
65          # Make dir relative to installdir. (Works only if dir is absolute.)
66          idir="$installdir"
67          while true; do
68            dfirst=`echo "$dir" | sed -n -e 's,^//*\([^/]*\).*$,/\1,p'`
69            ifirst=`echo "$idir" | sed -n -e 's,^//*\([^/]*\).*$,/\1,p'`
70            if test -z "$dfirst" || test -z "$ifirst"; then
71              break
72            fi
73            if test "$dfirst" != "$ifirst"; then
74              break
75            fi
76            dir=`echo "$dir" | sed -e 's,^//*[^/]*,,'`
77            idir=`echo "$idir" | sed -e 's,^//*[^/]*,,'`
78          done
79          dir="\$ORIGIN"`echo "$idir" | sed -e 's,//*[^/]*,/..,g'`"$dir"
80          # Add dir to rpath.
81          rpath="${rpath}${rpath:+ }$dir"
82          ;;
83        *)
84          if test -n "$dir"; then
85            echo "libdir is not absolute: $dir" 1>&2
86          fi
87          ;;
88      esac
89    done
90    IFS="$save_IFS"
91    # Output it.
92    if test -n "$rpath"; then
93      echo "-Wl,-rpath,$rpath"
94    fi
95    ;;
96  *)
97    echo "relocation via rpath not supported on this system: $host" 1>&2
98    exit 1
99    ;;
100esac
101
102exit 0
103