| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # Script to install host system binaries along with required libraries. |
| 4 | # Refer to the --help output for more information. |
| 5 | # |
| 6 | # Copyright (C) 2012 Jo-Philipp Wich <jow@openwrt.org> |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this program; if not, write to the Free Software |
| 20 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | DIR="$1"; shift |
| 23 | |
| 24 | _cp() { |
| 25 | cp ${VERBOSE:+-v} -L "$1" "$2" || { |
| 26 | echo "cp($1 $2) failed" >&2 |
| 27 | exit 1 |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | _md() { |
| 32 | mkdir ${VERBOSE:+-v} -p "$1" || { |
| 33 | echo "mkdir($1) failed" >&2 |
| 34 | exit 2 |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | _ln() { |
| 39 | ln ${VERBOSE:+-v} -sf "$1" "$2" || { |
| 40 | echo "ln($1 $2) failed" >&2 |
| 41 | exit 3 |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | for LDD in ${PATH//://ldd }/ldd; do |
| 46 | "$LDD" --version >/dev/null 2>/dev/null && break |
| 47 | LDD="" |
| 48 | done |
| 49 | |
| 50 | [ -n "$LDD" -a -x "$LDD" ] || { |
| 51 | echo "Unable to find working ldd" >&2 |
| 52 | exit 4 |
| 53 | } |
| 54 | |
| 55 | for BIN in "$@"; do |
| 56 | [ -n "$BIN" -a -x "$BIN" -a -n "$DIR" ] || { |
| 57 | echo "Usage: $0 <destdir> <executable> ..." >&2 |
| 58 | exit 1 |
| 59 | } |
| 60 | |
| 61 | [ ! -d "$DIR/bundled/lib" ] && { |
| 62 | _md "$DIR/bundled/lib" |
| 63 | _md "$DIR/bundled/usr" |
| 64 | _ln "../lib" "$DIR/bundled/usr/lib" |
| 65 | } |
| 66 | |
| 67 | LDSO="" |
| 68 | |
| 69 | echo "Bundling ${BIN##*/}" |
| 70 | for token in $("$LDD" "$BIN" 2>/dev/null); do |
| 71 | case "$token" in */*.so*) |
| 72 | case "$token" in |
| 73 | *ld-*.so*) LDSO="${token##*/}" ;; |
| 74 | *) echo " * lib: ${token##*/}" ;; |
| 75 | esac |
| 76 | |
| 77 | dest="$DIR/bundled/lib/${token##*/}" |
| 78 | ddir="${dest%/*}" |
| 79 | |
| 80 | [ -f "$token" -a ! -f "$dest" ] && { |
| 81 | _md "$ddir" |
| 82 | _cp "$token" "$dest" |
| 83 | } |
| 84 | ;; esac |
| 85 | done |
| 86 | |
| 87 | _md "$DIR" |
| 88 | |
| 89 | # is a dynamically linked executable |
| 90 | if [ -n "$LDSO" ]; then |
| 91 | _cp "$BIN" "$DIR/bundled/${BIN##*/}" |
| 92 | |
| 93 | [ -x "$DIR/bundled/run.sh" ] || { |
| 94 | cat <<-EOF > "$DIR/bundled/run.sh" |
| 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.sh" |
| 101 | } |
| 102 | |
| 103 | _ln "./bundled/run.sh" "$DIR/${BIN##*/}" |
| 104 | |
| 105 | # is a static executable or non-elf binary |
| 106 | else |
| 107 | echo " * not dynamically linked" |
| 108 | _cp "$BIN" "$DIR/${BIN##*/}" |
| 109 | fi |
| 110 | done |
| 111 | |