New modules relocatable, relocatable-lib, relocatable-script.
[gnulib.git] / build-aux / install-reloc
1 #!/bin/sh
2 # install-reloc - install a program including a relocating wrapper
3 # Copyright (C) 2003, 2005-2006 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 compile_command srcdir config_h_dir exeext install_command... destprog
22 # where
23 #   - library_path_var is the platform dependent runtime library path variable
24 #   - library_path_value is a colon separated list of directories that contain
25 #     the libraries at installation time (use this instead of -rpath)
26 #   - prefix is the base directory at installation time
27 #   - compile_command is a C compiler compilation and linking command
28 #   - srcdir is the directory where to find relocwrapper.c and its dependencies
29 #   - builddir is the directory where to find built dependencies (namely,
30 #     alloca.h and stdbool.h)
31 #   - config_h_dir is the directory where to find config.h
32 #   - exeext is platform dependent suffix of executables
33 #   - install-command is the install command line, excluding the final destprog
34 #   - destprog is the destination program name
35 # install-reloc renames destprog to destprog.bin and installs a relocating
36 # wrapper in the place of destprog.
37
38 progname=$0
39
40 if test $# -eq 2; then
41   # Get arguments from environment variables.
42   library_path_var=$RELOC_LIBRARY_PATH_VAR
43   library_path_value=$RELOC_LIBRARY_PATH_VALUE
44   prefix=$RELOC_PREFIX
45   compile_command=$RELOC_COMPILE_COMMAND
46   srcdir=$RELOC_SRCDIR
47   builddir=$RELOC_BUILDDIR
48   config_h_dir=$RELOC_CONFIG_H_DIR
49   exeext=$RELOC_EXEEXT
50   install_prog=$RELOC_INSTALL_PROG # including the "-c" option
51 else
52   if test $# -ge 9; then
53     # Get fixed position arguments.
54     library_path_var=$1
55     library_path_value=$2
56     prefix=$3
57     compile_command=$4
58     srcdir=$5
59     builddir=$6
60     config_h_dir=$7
61     exeext=$8
62     install_prog=$9 # maybe not including the "-c" option
63     shift
64     shift
65     shift
66     shift
67     shift
68     shift
69     shift
70     shift
71     shift
72   else
73     echo "Usage: $0 library_path_var library_path_value prefix compile_command srcdir builddir config_h_dir exeext install_command... destprog" 1>&2
74     exit 1
75   fi
76 fi
77
78 # Get destprog, last argument.
79 destprog=
80 for arg
81 do
82   destprog=$arg
83 done
84 # Remove trailing $exeext, if present.
85 if test -n "$exeext"; then
86   sedexpr='s|'`echo "$exeext" | sed -e 's,\.,\\\.,g'`'$||'
87   destprog=`echo "$destprog" | sed -e "$sedexpr"`
88 fi
89
90 # Outputs a command and runs it.
91 func_verbose ()
92 {
93   echo "$@"
94   "$@"
95 }
96
97 # Run install_command.
98 func_verbose $install_prog "$@" || exit $?
99
100 # If the platform doesn't support LD_LIBRARY_PATH or similar, we cannot build
101 # a wrapper.
102 test -n "$library_path_var" || exit 0
103
104 libdirs=
105 save_IFS="$IFS"; IFS=":"
106 for dir in $library_path_value; do
107   IFS="$save_IFS"
108   if test -n "$dir"; then
109     case "$libdirs" in
110       *"\"$dir\""*) ;; # remove duplicate
111       *) libdirs="$libdirs\"$dir\"," ;;
112     esac
113   fi
114 done
115 IFS="$save_IFS"
116 # If there are no library directories to add at runtime, we don't need a
117 # wrapper.
118 test -n "$libdirs" || exit 0
119
120 # Compile wrapper.
121 installdir=`echo "$destprog" | sed -e 's,/[^/]*$,,'`
122 func_verbose $compile_command -I"$builddir" -I"$srcdir" -I"$config_h_dir" -DHAVE_CONFIG_H -DIN_RELOCWRAPPER -DNO_XMALLOC -D"INSTALLPREFIX=\"$prefix\"" -D"INSTALLDIR=\"$installdir\"" -D"LIBPATHVAR=\"$library_path_var\"" -D"LIBDIRS=$libdirs" -D"EXEEXT=\"$exeext\"" "$srcdir"/relocwrapper.c "$srcdir"/progname.c "$srcdir"/progreloc.c "$srcdir"/xreadlink.c "$srcdir"/readlink.c "$srcdir"/canonicalize-lgpl.c "$srcdir"/allocsa.c "$srcdir"/relocatable.c "$srcdir"/setenv.c "$srcdir"/strerror.c "$srcdir"/c-ctype.c -o "$destprog.wrapper$exeext" || exit $?
123
124 # Rename $destprog.wrapper -> $destprog -> $destprog.bin.
125 ln -f "$destprog$exeext" "$destprog.bin$exeext" \
126   || { rm -f "$destprog.bin$exeext" && cp -p "$destprog$exeext" "$destprog.bin$exeext"; } \
127   || exit 1
128 mv "$destprog.wrapper$exeext" "$destprog$exeext" || exit 1
129
130 exit 0