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