Fix link error in relocatability wrappers, due to xalloc_die().
[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 \
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 #   - compile_command is a C compiler compilation and linking command
29 #   - srcdir is the directory where to find relocwrapper.c and its dependencies
30 #   - builddir is the directory where to find built dependencies (namely,
31 #     alloca.h and stdbool.h)
32 #   - config_h_dir is the directory where to find config.h
33 #   - exeext is platform dependent suffix of executables
34 #   - install-command is the install command line, excluding the final destprog
35 #   - destprog is the destination program name
36 # install-reloc renames destprog to destprog.bin and installs a relocating
37 # wrapper in the place of destprog.
38
39 progname=$0
40
41 if test $# -eq 2; then
42   # Get arguments from environment variables.
43   library_path_var=$RELOC_LIBRARY_PATH_VAR
44   library_path_value=$RELOC_LIBRARY_PATH_VALUE
45   prefix=$RELOC_PREFIX
46   compile_command=$RELOC_COMPILE_COMMAND
47   srcdir=$RELOC_SRCDIR
48   builddir=$RELOC_BUILDDIR
49   config_h_dir=$RELOC_CONFIG_H_DIR
50   exeext=$RELOC_EXEEXT
51   install_prog=$RELOC_INSTALL_PROG # including the "-c" option
52 else
53   if test $# -ge 9; then
54     # Get fixed position arguments.
55     library_path_var=$1
56     library_path_value=$2
57     prefix=$3
58     compile_command=$4
59     srcdir=$5
60     builddir=$6
61     config_h_dir=$7
62     exeext=$8
63     install_prog=$9 # maybe not including the "-c" option
64     shift
65     shift
66     shift
67     shift
68     shift
69     shift
70     shift
71     shift
72     shift
73   else
74     echo "Usage: $0 library_path_var library_path_value prefix" \
75          "compile_command srcdir builddir config_h_dir exeext" \
76          "install_command... destprog" 1>&2
77     exit 1
78   fi
79 fi
80
81 # Get destprog, last argument.
82 destprog=
83 for arg
84 do
85   destprog=$arg
86 done
87 # Remove trailing $exeext, if present.
88 if test -n "$exeext"; then
89   sedexpr='s|'`echo "$exeext" | sed -e 's,\.,\\\.,g'`'$||'
90   destprog=`echo "$destprog" | sed -e "$sedexpr"`
91 fi
92
93 # Outputs a command and runs it.
94 func_verbose ()
95 {
96   echo "$@"
97   "$@"
98 }
99
100 # Run install_command.
101 func_verbose $install_prog "$@" || exit $?
102
103 # If the platform doesn't support LD_LIBRARY_PATH or similar, we cannot build
104 # a wrapper.
105 test -n "$library_path_var" || exit 0
106
107 libdirs=
108 save_IFS="$IFS"; IFS=":"
109 for dir in $library_path_value; do
110   IFS="$save_IFS"
111   if test -n "$dir"; then
112     case "$libdirs" in
113       *"\"$dir\""*) ;; # remove duplicate
114       *) libdirs="$libdirs\"$dir\"," ;;
115     esac
116   fi
117 done
118 IFS="$save_IFS"
119 # If there are no library directories to add at runtime, we don't need a
120 # wrapper.
121 test -n "$libdirs" || exit 0
122
123 # Compile wrapper.
124 installdir=`echo "$destprog" | sed -e 's,/[^/]*$,,'`
125 func_verbose $compile_command \
126              -I"$builddir" -I"$srcdir" -I"$config_h_dir" \
127              -DHAVE_CONFIG_H -DIN_RELOCWRAPPER -DNO_XMALLOC \
128              -D"INSTALLPREFIX=\"$prefix\"" -D"INSTALLDIR=\"$installdir\"" \
129              -D"LIBPATHVAR=\"$library_path_var\"" -D"LIBDIRS=$libdirs" \
130              -D"EXEEXT=\"$exeext\"" \
131              "$srcdir"/relocwrapper.c \
132              "$srcdir"/progname.c \
133              "$srcdir"/progreloc.c \
134              "$srcdir"/areadlink.c \
135              "$srcdir"/readlink.c \
136              "$srcdir"/canonicalize-lgpl.c \
137              "$srcdir"/malloca.c \
138              "$srcdir"/relocatable.c \
139              "$srcdir"/setenv.c \
140              "$srcdir"/strerror.c \
141              "$srcdir"/c-ctype.c \
142              -o "$destprog.wrapper$exeext"
143 rc=$?
144 # Clean up object files left over in the current directory by the native C
145 # compilers on Solaris, HP-UX, OSF/1, IRIX.
146 rm -f relocwrapper.o \
147       progname.o \
148       progreloc.o \
149       xreadlink.o \
150       areadlink.o \
151       canonicalize-lgpl.o \
152       malloca.o \
153       relocatable.o \
154       setenv.o \
155       strerror.o \
156       c-ctype.o
157 test $rc = 0 || exit $?
158
159 # Rename $destprog.wrapper -> $destprog -> $destprog.bin.
160 ln -f "$destprog$exeext" "$destprog.bin$exeext" \
161   || { rm -f "$destprog.bin$exeext" \
162        && cp -p "$destprog$exeext" "$destprog.bin$exeext"; } \
163   || exit 1
164 mv "$destprog.wrapper$exeext" "$destprog$exeext" || exit 1
165
166 exit 0