2 # install-reloc - install a program including a relocating wrapper
3 # Copyright (C) 2003-2018 Free Software Foundation, Inc.
4 # Written by Bruno Haible <bruno@clisp.org>, 2003.
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.
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.
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/>.
20 # install-reloc -- library_path_var library_path_value prefix destdir \
21 # compile_command srcdir builddir config_h_dir exeext \
23 # install_command... destprog
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
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
43 # env RELOC_LIBRARY_PATH_VAR=library_path_var \
44 # RELOC_LIBRARY_PATH_VALUE=library_path_value \
45 # RELOC_PREFIX=prefix \
46 # RELOC_DESTDIR=destdir \
47 # RELOC_COMPILE_COMMAND=compile_command \
48 # RELOC_SRCDIR=srcdir \
49 # RELOC_BUILDDIR=builddir \
50 # RELOC_CONFIG_H_DIR=config_h_dir \
51 # RELOC_EXEEXT=exeext \
52 # RELOC_STRIP_PROG=strip_command \
53 # RELOC_INSTALL_PROG=install_command... \
54 # install-reloc prog1 ... destprog
55 # where destprog is either the destination program name (when only one program
56 # is specified) or the destination directory for all programs.
57 # install-reloc renames destprog to destprog.bin and installs a relocating
58 # wrapper in the place of destprog.
62 if test $# -ge 12 && test "x$1" = "x--"; then
63 # Get fixed position arguments.
85 install_prog
=$1 # maybe not including the "-c" option
88 if test $# -ge 2; then
89 # Get arguments from environment variables.
90 library_path_var
=$RELOC_LIBRARY_PATH_VAR
91 library_path_value
=$RELOC_LIBRARY_PATH_VALUE
93 destdir
=$RELOC_DESTDIR
94 compile_command
=$RELOC_COMPILE_COMMAND
96 builddir
=$RELOC_BUILDDIR
97 config_h_dir
=$RELOC_CONFIG_H_DIR
99 strip_prog
=$RELOC_STRIP_PROG
100 install_prog
=$RELOC_INSTALL_PROG # including the "-c" option
102 echo "Usage: $0 -- library_path_var library_path_value prefix destdir" \
103 "compile_command srcdir builddir config_h_dir exeext" \
105 "install_command... destprog" 1>&2
110 # Get destprog, last argument.
116 # Determine whether destprog is a program name or a directory name.
117 if test -d "$destprog"; then
118 sed_remove_trailing_slashes
='s|//*$||'
119 destprog_directory
=`echo "$destprog" | sed -e "$sed_remove_trailing_slashes"`
120 if test -z "$destprog_directory"; then
121 destprog_directory
='/'
126 # Prepare for remove trailing $exeext, if present.
127 if test -n "$exeext"; then
128 sed_quote
='s,\.,\\.,g'
129 sed_remove_exeext
='s|'`echo "$exeext" | sed -e "$sed_quote"`'$||'
131 if test -z "$destprog_directory"; then
132 # Remove trailing $exeext, if present.
133 if test -n "$exeext"; then
134 destprog
=`echo "$destprog" | sed -e "$sed_remove_exeext"`
138 # Outputs a command and runs it.
145 # Run install_command.
146 func_verbose
$install_prog "$@" ||
exit $?
148 # Iterate over all destination program names.
150 # applies f to each destination program names, after setting destprog.
151 sed_basename_of_file
='s|^.*/||'
154 if test -n "$destprog_directory"; then
158 if test -n "prev_arg"; then
159 destprog
="$destprog_directory"/`echo "$prev_arg" | sed -e "$sed_basename_of_file"`
172 # Remove trailing $exeext, if present.
173 if test -n "$exeext"; then
174 destprog
=`echo "$destprog" | sed -e "$sed_remove_exeext"`
176 func_verbose
"$strip_prog" "$destprog$exeext" ||
exit $?
178 if test "$strip_prog" != ':'; then
179 func_iterate func_strip
182 # If the platform doesn't support LD_LIBRARY_PATH or similar, we cannot build
184 test -n "$library_path_var" ||
exit 0
187 save_IFS
="$IFS"; IFS
=":"
188 for dir
in $library_path_value; do
190 if test -n "$dir"; then
192 *"\"$dir\""*) ;; # remove duplicate
193 *) libdirs
="$libdirs\"$dir\"," ;;
198 # If there are no library directories to add at runtime, we don't need a
200 test -n "$libdirs" ||
exit 0
202 # Determine installdir from destprog, removing a leading destdir if present.
203 if test -n "$destprog_directory"; then
204 installdir
="$destprog_directory"
206 installdir
=`echo "$destprog" | sed -e 's,/[^/]*$,,'`
208 if test -n "$destdir"; then
209 sed_quote
='s,\([|.\*^$[]\),\\\1,g'
210 sed_remove_destdir
='s|^'`echo "$destdir" | sed -e "$sed_quote"`'||'
211 installdir
=`echo "$installdir" | sed -e "$sed_remove_destdir"`
214 # Compile and install wrapper.
215 func_create_wrapper
()
217 # Remove trailing $exeext, if present.
218 if test -n "$exeext"; then
219 destprog
=`echo "$destprog" | sed -e "$sed_remove_exeext"`
223 func_verbose
$compile_command \
224 -I"$builddir" -I"$srcdir" -I"$config_h_dir" \
225 -DHAVE_CONFIG_H -DIN_RELOCWRAPPER -DNO_XMALLOC \
226 -D"INSTALLPREFIX=\"$prefix\"" -D"INSTALLDIR=\"$installdir\"" \
227 -D"LIBPATHVAR=\"$library_path_var\"" -D"LIBDIRS=$libdirs" \
228 -D"EXEEXT=\"$exeext\"" \
229 "$srcdir"/relocwrapper.c \
230 "$srcdir"/progname.c \
231 "$srcdir"/progreloc.c \
232 "$srcdir"/areadlink.c \
233 "$srcdir"/careadlinkat.c \
234 "$srcdir"/allocator.c \
235 "$srcdir"/readlink.c \
237 "$srcdir"/canonicalize-lgpl.c \
238 "$srcdir"/malloca.c \
240 "$srcdir"/relocatable.c \
242 "$srcdir"/c-ctype.c \
243 -o "$destprog.wrapper$exeext"
245 # Clean up object files left over in the current directory by the native C
246 # compilers on Solaris, HP-UX, OSF/1, IRIX.
247 rm -f relocwrapper.o \
255 canonicalize-lgpl.o \
261 test $rc = 0 ||
exit $?
262 # Clean up debugging information left over by the native C compiler on MacOS X.
263 rm -rf "$destprog.wrapper$exeext.dSYM"
264 test $rc = 0 ||
exit $?
267 test "$strip_prog" = ':' || func_verbose
"$strip_prog" "$destprog.wrapper$exeext" ||
exit $?
269 # Rename $destprog.wrapper -> $destprog -> $destprog.bin.
270 ln -f "$destprog$exeext" "$destprog.bin$exeext" \
271 ||
{ rm -f "$destprog.bin$exeext" \
272 && cp -p "$destprog$exeext" "$destprog.bin$exeext"; } \
274 mv "$destprog.wrapper$exeext" "$destprog$exeext" ||
exit 1
276 func_iterate func_create_wrapper