3 # Script to install host system binaries along with required libraries.
5 # Copyright (C) 2012-2017 Jo-Philipp Wich <jo@mein.io>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 cp ${VERBOSE:+-v} -L "$1" "$2" ||
{
25 echo "cp($1 $2) failed" >&2
31 mv ${VERBOSE:+-v} "$1" "$2" ||
{
32 echo "mv($1 $2) failed" >&2
38 mkdir
${VERBOSE:+-v} -p "$1" ||
{
39 echo "mkdir($1) failed" >&2
45 ln ${VERBOSE:+-v} -sf "$1" "$2" ||
{
46 echo "ln($1 $2) failed" >&2
52 local base
="$(readlink -f "$1")"
53 local dest
="$(readlink -f "$2")"
56 [ -d "$base" ] || base
="${base%/*}"
57 [ -d "$dest" ] || dest
="${dest%/*}"
62 echo "$up/${base#$dest/}"
74 cat <<-EOT | ${CC:-gcc} -x c -o "$1" -
78 int main(int argc, char **argv) {
79 const char *self = argv[0];
80 const char *target = argv[1];
83 fprintf(stderr, "Usage: %s executable arg0 [args...]\n", self);
87 return execv(target, argv + 2);
92 echo "compiling wrapper failed" >&2
97 for LDD
in ${PATH//://ldd }/ldd
; do
98 "$LDD" --version >/dev
/null
2>/dev
/null
&& break
102 [ -n "$LDD" -a -x "$LDD" ] || LDD
=
105 [ -n "$BIN" -a -n "$DIR" ] ||
{
106 echo "Usage: $0 <destdir> <executable> ..." >&2
110 [ ! -d "$DIR/lib" ] && {
113 _ln
"../lib" "$DIR/usr/lib"
116 [ ! -x "$DIR/lib/runas" ] && {
117 _wrapper
"$DIR/lib/runas"
122 [ -n "$LDD" ] && [ -x "$BIN" ] && file "$BIN" |
grep -sqE "ELF.*executable" && {
123 for token
in $
("$LDD" "$BIN" 2>/dev
/null
); do
124 case "$token" in */*.so
*)
126 *ld-
*.so
*) LDSO
="${token##*/}" ;;
129 dest
="$DIR/lib/${token##*/}"
132 [ -f "$token" -a ! -f "$dest" ] && {
140 # is a dynamically linked executable
141 if [ -n "$LDSO" ]; then
142 echo "Bundling ${BIN##*/}"
144 RUNDIR
="$(readlink -f "$BIN")"; RUNDIR
="${RUNDIR%/*}"
145 RUN
="${LDSO#ld-}"; RUN
="run-${RUN%%.so*}.sh"
146 REL
="$(_relpath "$DIR/lib
" "$BIN")"
148 _mv
"$BIN" "$RUNDIR/.${BIN##*/}.bin"
152 dir="\$(dirname "\$0")"
153 exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/${REL:+$REL/}runas" "\$dir/.${BIN##*/}.bin" "\$0" "\$@"
156 chmod ${VERBOSE:+-v} 0755 "$BIN"