Make --enable-relocatable work with DESTDIR.
[gnulib.git] / build-aux / install-reloc
1 #!/bin/sh
2 # install-reloc - install a program including a relocating wrapper
3 # Copyright (C) 2003, 2005-2007 Free Software Foundation, Inc.
4 # Written by Bruno Haible <bruno@clisp.org>, 2003.
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 <http://www.gnu.org/licenses/>.
18
19 # Usage:
20 #   install-reloc library_path_var library_path_value prefix destdir \
21 #                 compile_command srcdir config_h_dir exeext \
22 #                 install_command... destprog
23 # where
24 #   - library_path_var is the platform dependent runtime library path variable
25 #   - library_path_value is a colon separated list of directories that contain
26 #     the libraries at installation time (use this instead of -rpath)
27 #   - prefix is the base directory at installation time
28 #   - destdir is a string that is prepended to all file names at installation
29 #     time; it is already prepended to destprog but not to library_path_value
30 #     and prefix
31 #   - compile_command is a C compiler compilation and linking command
32 #   - srcdir is the directory where to find relocwrapper.c and its dependencies
33 #   - builddir is the directory where to find built dependencies (namely,
34 #     alloca.h and stdbool.h)
35 #   - config_h_dir is the directory where to find config.h
36 #   - exeext is platform dependent suffix of executables
37 #   - install-command is the install command line, excluding the final destprog
38 #   - destprog is the destination program name
39 # install-reloc renames destprog to destprog.bin and installs a relocating
40 # wrapper in the place of destprog.
41
42 progname=$0
43
44 if test $# -eq 2; then
45   # Get arguments from environment variables.
46   library_path_var=$RELOC_LIBRARY_PATH_VAR
47   library_path_value=$RELOC_LIBRARY_PATH_VALUE
48   prefix=$RELOC_PREFIX
49   destdir=$RELOC_DESTDIR
50   compile_command=$RELOC_COMPILE_COMMAND
51   srcdir=$RELOC_SRCDIR
52   builddir=$RELOC_BUILDDIR
53   config_h_dir=$RELOC_CONFIG_H_DIR
54   exeext=$RELOC_EXEEXT
55   install_prog=$RELOC_INSTALL_PROG # including the "-c" option
56 else
57   if test $# -ge 10; then
58     # Get fixed position arguments.
59     library_path_var=$1
60     library_path_value=$2
61     prefix=$3
62     destdir=$4
63     compile_command=$5
64     srcdir=$6
65     builddir=$7
66     config_h_dir=$8
67     exeext=$9
68     shift
69     shift
70     shift
71     shift
72     shift
73     shift
74     shift
75     shift
76     shift
77     install_prog=$1 # maybe not including the "-c" option
78     shift
79   else
80     echo "Usage: $0 library_path_var library_path_value prefix destdir" \
81          "compile_command srcdir builddir config_h_dir exeext" \
82          "install_command... destprog" 1>&2
83     exit 1
84   fi
85 fi
86
87 # Get destprog, last argument.
88 destprog=
89 for arg
90 do
91   destprog=$arg
92 done
93 # Remove trailing $exeext, if present.
94 if test -n "$exeext"; then
95   sed_quote='s,\.,\\.,g'
96   sed_remove_exeext='s|'`echo "$exeext" | sed -e "$sed_quote"`'$||'
97   destprog=`echo "$destprog" | sed -e "$sed_remove_exeext"`
98 fi
99
100 # Outputs a command and runs it.
101 func_verbose ()
102 {
103   echo "$@"
104   "$@"
105 }
106
107 # Run install_command.
108 func_verbose $install_prog "$@" || exit $?
109
110 # If the platform doesn't support LD_LIBRARY_PATH or similar, we cannot build
111 # a wrapper.
112 test -n "$library_path_var" || exit 0
113
114 libdirs=
115 save_IFS="$IFS"; IFS=":"
116 for dir in $library_path_value; do
117   IFS="$save_IFS"
118   if test -n "$dir"; then
119     case "$libdirs" in
120       *"\"$dir\""*) ;; # remove duplicate
121       *) libdirs="$libdirs\"$dir\"," ;;
122     esac
123   fi
124 done
125 IFS="$save_IFS"
126 # If there are no library directories to add at runtime, we don't need a
127 # wrapper.
128 test -n "$libdirs" || exit 0
129
130 # Determine installdir from destprog, removing a leading destdir if present.
131 installdir=`echo "$destprog" | sed -e 's,/[^/]*$,,'`
132 if test -n "$destdir"; then
133   sed_quote='s,\([|.\*^$[]\),\\\1,g'
134   sed_remove_destdir='s|^'`echo "$destdir" | sed -e "$sed_quote"`'||'
135   installdir=`echo "$installdir" | sed -e "$sed_remove_destdir"`
136 fi
137
138 # Compile wrapper.
139 func_verbose $compile_command \
140              -I"$builddir" -I"$srcdir" -I"$config_h_dir" \
141              -DHAVE_CONFIG_H -DIN_RELOCWRAPPER -DNO_XMALLOC \
142              -D"INSTALLPREFIX=\"$prefix\"" -D"INSTALLDIR=\"$installdir\"" \
143              -D"LIBPATHVAR=\"$library_path_var\"" -D"LIBDIRS=$libdirs" \
144              -D"EXEEXT=\"$exeext\"" \
145              "$srcdir"/relocwrapper.c \
146              "$srcdir"/progname.c \
147              "$srcdir"/progreloc.c \
148              "$srcdir"/areadlink.c \
149              "$srcdir"/readlink.c \
150              "$srcdir"/canonicalize-lgpl.c \
151              "$srcdir"/malloca.c \
152              "$srcdir"/relocatable.c \
153              "$srcdir"/setenv.c \
154              "$srcdir"/strerror.c \
155              "$srcdir"/c-ctype.c \
156              -o "$destprog.wrapper$exeext"
157 rc=$?
158 # Clean up object files left over in the current directory by the native C
159 # compilers on Solaris, HP-UX, OSF/1, IRIX.
160 rm -f relocwrapper.o \
161       progname.o \
162       progreloc.o \
163       xreadlink.o \
164       areadlink.o \
165       canonicalize-lgpl.o \
166       malloca.o \
167       relocatable.o \
168       setenv.o \
169       strerror.o \
170       c-ctype.o
171 test $rc = 0 || exit $?
172
173 # Rename $destprog.wrapper -> $destprog -> $destprog.bin.
174 ln -f "$destprog$exeext" "$destprog.bin$exeext" \
175   || { rm -f "$destprog.bin$exeext" \
176        && cp -p "$destprog$exeext" "$destprog.bin$exeext"; } \
177   || exit 1
178 mv "$destprog.wrapper$exeext" "$destprog$exeext" || exit 1
179
180 exit 0