1#!/usr/bin/env bash 2# 3# Script to install host system binaries along with required libraries. 4# 5# Copyright (C) 2012-2013 Jo-Philipp Wich <jow@openwrt.org> 6# 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. 11# 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. 16# 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 20 21DIR="$1"; shift 22 23_cp() { 24 cp ${VERBOSE:+-v} -L "$1" "$2" || { 25 echo "cp($1 $2) failed" >&2 26 exit 1 27 } 28} 29 30_md() { 31 mkdir ${VERBOSE:+-v} -p "$1" || { 32 echo "mkdir($1) failed" >&2 33 exit 2 34 } 35} 36 37_ln() { 38 ln ${VERBOSE:+-v} -sf "$1" "$2" || { 39 echo "ln($1 $2) failed" >&2 40 exit 3 41 } 42} 43 44for LDD in ${PATH//://ldd }/ldd; do 45 "$LDD" --version >/dev/null 2>/dev/null && break 46 LDD="" 47done 48 49[ -n "$LDD" -a -x "$LDD" ] || LDD= 50 51for BIN in "$@"; do 52 [ -n "$BIN" -a -x "$BIN" -a -n "$DIR" ] || { 53 echo "Usage: $0 <destdir> <executable> ..." >&2 54 exit 1 55 } 56 57 [ ! -d "$DIR/bundled/lib" ] && { 58 _md "$DIR/bundled/lib" 59 _md "$DIR/bundled/usr" 60 _ln "../lib" "$DIR/bundled/usr/lib" 61 } 62 63 LDSO="" 64 65 echo "Bundling ${BIN##*/}" 66 [ -n "$LDD" ] && { 67 for token in $("$LDD" "$BIN" 2>/dev/null); do 68 case "$token" in */*.so*) 69 case "$token" in 70 *ld-*.so*) LDSO="${token##*/}" ;; 71 *) echo " * lib: ${token##*/}" ;; 72 esac 73 74 dest="$DIR/bundled/lib/${token##*/}" 75 ddir="${dest%/*}" 76 77 [ -f "$token" -a ! -f "$dest" ] && { 78 _md "$ddir" 79 _cp "$token" "$dest" 80 } 81 ;; esac 82 done 83 } 84 85 _md "$DIR" 86 87 # is a dynamically linked executable 88 if [ -n "$LDSO" ]; then 89 _cp "$BIN" "$DIR/bundled/${BIN##*/}" 90 91 RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh" 92 93 [ -x "$DIR/bundled/$RUN" ] || { 94 cat <<-EOF > "$DIR/bundled/$RUN" 95 #!/usr/bin/env bash 96 dir="\$(dirname "\$0")" 97 bin="\$(basename "\$0")" 98 exec -a "\$0" "\$dir/bundled/lib/$LDSO" --library-path "\$dir/bundled/lib" "\$dir/bundled/\$bin" "\$@" 99 EOF 100 chmod ${VERBOSE:+-v} 0755 "$DIR/bundled/$RUN" 101 } 102 103 _ln "./bundled/$RUN" "$DIR/${BIN##*/}" 104 105 # is a static executable or non-elf binary 106 else 107 [ -n "$LDD" ] && echo " * not dynamically linked" 108 _cp "$BIN" "$DIR/${BIN##*/}" 109 fi 110done 111