xref: /netbsd-src/external/gpl2/gettext/dist/build-aux/install-reloc (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1*946379e7Schristos#!/bin/sh
2*946379e7Schristos# install-reloc - install a program including a relocating wrapper
3*946379e7Schristos# Copyright (C) 2003, 2005-2006 Free Software Foundation, Inc.
4*946379e7Schristos# Written by Bruno Haible <bruno@clisp.org>, 2003.
5*946379e7Schristos#
6*946379e7Schristos# This program is free software; you can redistribute it and/or modify
7*946379e7Schristos# it under the terms of the GNU General Public License as published by
8*946379e7Schristos# the Free Software Foundation; either version 2, or (at your option)
9*946379e7Schristos# any later version.
10*946379e7Schristos#
11*946379e7Schristos# This program is distributed in the hope that it will be useful,
12*946379e7Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
13*946379e7Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*946379e7Schristos# GNU General Public License for more details.
15*946379e7Schristos#
16*946379e7Schristos# You should have received a copy of the GNU General Public License
17*946379e7Schristos# along with this program; if not, write to the Free Software Foundation,
18*946379e7Schristos# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19*946379e7Schristos
20*946379e7Schristos# Usage:
21*946379e7Schristos#   install-reloc library_path_var library_path_value prefix compile_command srcdir config_h_dir exeext install_command... destprog
22*946379e7Schristos# where
23*946379e7Schristos#   - library_path_var is the platform dependent runtime library path variable
24*946379e7Schristos#   - library_path_value is a colon separated list of directories that contain
25*946379e7Schristos#     the libraries at installation time (use this instead of -rpath)
26*946379e7Schristos#   - prefix is the base directory at installation time
27*946379e7Schristos#   - compile_command is a C compiler compilation and linking command
28*946379e7Schristos#   - srcdir is the directory where to find relocwrapper.c and its dependencies
29*946379e7Schristos#   - builddir is the directory where to find built dependencies (namely,
30*946379e7Schristos#     alloca.h and stdbool.h)
31*946379e7Schristos#   - config_h_dir is the directory where to find config.h
32*946379e7Schristos#   - exeext is platform dependent suffix of executables
33*946379e7Schristos#   - install-command is the install command line, excluding the final destprog
34*946379e7Schristos#   - destprog is the destination program name
35*946379e7Schristos# install-reloc renames destprog to destprog.bin and installs a relocating
36*946379e7Schristos# wrapper in the place of destprog.
37*946379e7Schristos
38*946379e7Schristosprogname=$0
39*946379e7Schristos
40*946379e7Schristosif test $# -eq 2; then
41*946379e7Schristos  # Get arguments from environment variables.
42*946379e7Schristos  library_path_var=$RELOC_LIBRARY_PATH_VAR
43*946379e7Schristos  library_path_value=$RELOC_LIBRARY_PATH_VALUE
44*946379e7Schristos  prefix=$RELOC_PREFIX
45*946379e7Schristos  compile_command=$RELOC_COMPILE_COMMAND
46*946379e7Schristos  srcdir=$RELOC_SRCDIR
47*946379e7Schristos  builddir=$RELOC_BUILDDIR
48*946379e7Schristos  config_h_dir=$RELOC_CONFIG_H_DIR
49*946379e7Schristos  exeext=$RELOC_EXEEXT
50*946379e7Schristos  install_prog=$RELOC_INSTALL_PROG # including the "-c" option
51*946379e7Schristoselse
52*946379e7Schristos  if test $# -ge 9; then
53*946379e7Schristos    # Get fixed position arguments.
54*946379e7Schristos    library_path_var=$1
55*946379e7Schristos    library_path_value=$2
56*946379e7Schristos    prefix=$3
57*946379e7Schristos    compile_command=$4
58*946379e7Schristos    srcdir=$5
59*946379e7Schristos    builddir=$6
60*946379e7Schristos    config_h_dir=$7
61*946379e7Schristos    exeext=$8
62*946379e7Schristos    install_prog=$9 # maybe not including the "-c" option
63*946379e7Schristos    shift
64*946379e7Schristos    shift
65*946379e7Schristos    shift
66*946379e7Schristos    shift
67*946379e7Schristos    shift
68*946379e7Schristos    shift
69*946379e7Schristos    shift
70*946379e7Schristos    shift
71*946379e7Schristos    shift
72*946379e7Schristos  else
73*946379e7Schristos    echo "Usage: $0 library_path_var library_path_value prefix compile_command srcdir builddir config_h_dir exeext install_command... destprog" 1>&2
74*946379e7Schristos    exit 1
75*946379e7Schristos  fi
76*946379e7Schristosfi
77*946379e7Schristos
78*946379e7Schristos# Get destprog, last argument.
79*946379e7Schristosdestprog=
80*946379e7Schristosfor arg
81*946379e7Schristosdo
82*946379e7Schristos  destprog=$arg
83*946379e7Schristosdone
84*946379e7Schristos# Remove trailing $exeext, if present.
85*946379e7Schristosif test -n "$exeext"; then
86*946379e7Schristos  sedexpr='s|'`echo "$exeext" | sed -e 's,\.,\\\.,g'`'$||'
87*946379e7Schristos  destprog=`echo "$destprog" | sed -e "$sedexpr"`
88*946379e7Schristosfi
89*946379e7Schristos
90*946379e7Schristos# Outputs a command and runs it.
91*946379e7Schristosfunc_verbose ()
92*946379e7Schristos{
93*946379e7Schristos  echo "$@"
94*946379e7Schristos  "$@"
95*946379e7Schristos}
96*946379e7Schristos
97*946379e7Schristos# Run install_command.
98*946379e7Schristosfunc_verbose $install_prog "$@" || exit $?
99*946379e7Schristos
100*946379e7Schristos# If the platform doesn't support LD_LIBRARY_PATH or similar, we cannot build
101*946379e7Schristos# a wrapper.
102*946379e7Schristostest -n "$library_path_var" || exit 0
103*946379e7Schristos
104*946379e7Schristoslibdirs=
105*946379e7Schristossave_IFS="$IFS"; IFS=":"
106*946379e7Schristosfor dir in $library_path_value; do
107*946379e7Schristos  IFS="$save_IFS"
108*946379e7Schristos  if test -n "$dir"; then
109*946379e7Schristos    case "$libdirs" in
110*946379e7Schristos      *"\"$dir\""*) ;; # remove duplicate
111*946379e7Schristos      *) libdirs="$libdirs\"$dir\"," ;;
112*946379e7Schristos    esac
113*946379e7Schristos  fi
114*946379e7Schristosdone
115*946379e7SchristosIFS="$save_IFS"
116*946379e7Schristos# If there are no library directories to add at runtime, we don't need a
117*946379e7Schristos# wrapper.
118*946379e7Schristostest -n "$libdirs" || exit 0
119*946379e7Schristos
120*946379e7Schristos# Compile wrapper.
121*946379e7Schristosinstalldir=`echo "$destprog" | sed -e 's,/[^/]*$,,'`
122*946379e7Schristosfunc_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.c "$srcdir"/allocsa.c "$srcdir"/relocatable.c "$srcdir"/setenv.c "$srcdir"/strerror.c -o "$destprog.wrapper$exeext" || exit $?
123*946379e7Schristos
124*946379e7Schristos# Rename $destprog.wrapper -> $destprog -> $destprog.bin.
125*946379e7Schristosln -f "$destprog$exeext" "$destprog.bin$exeext" \
126*946379e7Schristos  || { rm -f "$destprog.bin$exeext" && cp -p "$destprog$exeext" "$destprog.bin$exeext"; } \
127*946379e7Schristos  || exit 1
128*946379e7Schristosmv "$destprog.wrapper$exeext" "$destprog$exeext" || exit 1
129*946379e7Schristos
130*946379e7Schristosexit 0
131