[scripts] add a helper script to bundle required libraries for host utilities
[openwrt.git] / scripts / bundle-libraries.sh
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#*/lib*/}"
78                         ddir="${dest%/*}"
79
80                         [ -f "$token" -a ! -f "$dest" ] && {
81                                 _md "$ddir"
82                                 _cp "$token" "$dest"
83
84                                 case "$token" in */tls/*.so*)
85                                         _cp "${token%/tls/*}/${token##*/}" "$DIR/bundled/lib/${token##*/}"
86                                 ;; esac
87                         }
88                 ;; esac
89         done
90
91         _md "$DIR"
92
93         # is a dynamically linked executable
94         if [ -n "$LDSO" ]; then
95                 _cp "$BIN" "$DIR/bundled/${BIN##*/}"
96
97                 [ -x "$DIR/bundled/run.sh" ] || {
98                         cat <<-EOF > "$DIR/bundled/run.sh"
99                                 #!/usr/bin/env bash
100                                 dir="\$(dirname "\$0")"
101                                 bin="\$(basename "\$0")"
102                                 exec -a "\$0" "\$dir/bundled/lib/$LDSO" --library-path "\$dir/bundled/lib" "\$dir/bundled/\$bin" "\$@"
103                         EOF
104                         chmod ${VERBOSE:+-v} 0755 "$DIR/bundled/run.sh"
105                 }
106
107                 _ln "./bundled/run.sh" "$DIR/${BIN##*/}"
108
109         # is a static executable or non-elf binary
110         else
111                 echo " * not dynamically linked"
112                 _cp "$BIN" "$DIR/${BIN##*/}"
113         fi
114 done