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