[package] uboot-kirkwood: update to 2013.10
[openwrt.git] / scripts / bundle-libraries.sh
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
21 DIR="$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
44 for LDD in ${PATH//://ldd }/ldd; do
45         "$LDD" --version >/dev/null 2>/dev/null && break
46         LDD=""
47 done
48
49 [ -n "$LDD" -a -x "$LDD" ] || {
50         echo "Unable to find working ldd" >&2
51         exit 4
52 }
53
54 for BIN in "$@"; do
55         [ -n "$BIN" -a -x "$BIN" -a -n "$DIR" ] || {
56                 echo "Usage: $0 <destdir> <executable> ..." >&2
57                 exit 1
58         }
59
60         [ ! -d "$DIR/bundled/lib" ] && {
61                 _md "$DIR/bundled/lib"
62                 _md "$DIR/bundled/usr"
63                 _ln "../lib" "$DIR/bundled/usr/lib"
64         }
65
66         LDSO=""
67
68         echo "Bundling ${BIN##*/}"
69         for token in $("$LDD" "$BIN" 2>/dev/null); do
70                 case "$token" in */*.so*)
71                         case "$token" in
72                                 *ld-*.so*) LDSO="${token##*/}" ;;
73                                 *) echo " * lib: ${token##*/}" ;;
74                         esac
75
76                         dest="$DIR/bundled/lib/${token##*/}"
77                         ddir="${dest%/*}"
78
79                         [ -f "$token" -a ! -f "$dest" ] && {
80                                 _md "$ddir"
81                                 _cp "$token" "$dest"
82                         }
83                 ;; esac
84         done
85
86         _md "$DIR"
87
88         # is a dynamically linked executable
89         if [ -n "$LDSO" ]; then
90                 _cp "$BIN" "$DIR/bundled/${BIN##*/}"
91
92                 RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
93
94                 [ -x "$DIR/bundled/$RUN" ] || {
95                         cat <<-EOF > "$DIR/bundled/$RUN"
96                                 #!/usr/bin/env bash
97                                 dir="\$(dirname "\$0")"
98                                 bin="\$(basename "\$0")"
99                                 exec -a "\$0" "\$dir/bundled/lib/$LDSO" --library-path "\$dir/bundled/lib" "\$dir/bundled/\$bin" "\$@"
100                         EOF
101                         chmod ${VERBOSE:+-v} 0755 "$DIR/bundled/$RUN"
102                 }
103
104                 _ln "./bundled/$RUN" "$DIR/${BIN##*/}"
105
106         # is a static executable or non-elf binary
107         else
108                 echo " * not dynamically linked"
109                 _cp "$BIN" "$DIR/${BIN##*/}"
110         fi
111 done