rstrip.sh: fix handling variations in "file" output
[openwrt/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-2017 Jo-Philipp Wich <jo@mein.io>
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 _mv() {
31 mv ${VERBOSE:+-v} "$1" "$2" || {
32 echo "mv($1 $2) failed" >&2
33 exit 1
34 }
35 }
36
37 _md() {
38 mkdir ${VERBOSE:+-v} -p "$1" || {
39 echo "mkdir($1) failed" >&2
40 exit 2
41 }
42 }
43
44 _ln() {
45 ln ${VERBOSE:+-v} -sf "$1" "$2" || {
46 echo "ln($1 $2) failed" >&2
47 exit 3
48 }
49 }
50
51 _relpath() {
52 local base="$(readlink -f "$1")"
53 local dest="$(readlink -f "$2")"
54 local up
55
56 [ -d "$base" ] || base="${base%/*}"
57 [ -d "$dest" ] || dest="${dest%/*}"
58
59 while true; do
60 case "$base"
61 in "$dest"/*)
62 echo "$up/${base#$dest/}"
63 break
64 ;;
65 *)
66 dest="${dest%/*}"
67 up="${up:+$up/}.."
68 ;;
69 esac
70 done
71 }
72
73 _wrapper() {
74 cat <<-EOT | ${CC:-gcc} -x c -o "$1" -
75 #include <unistd.h>
76 #include <stdio.h>
77
78 int main(int argc, char **argv) {
79 const char *self = argv[0];
80 const char *target = argv[1];
81
82 if (argc < 3) {
83 fprintf(stderr, "Usage: %s executable arg0 [args...]\n", self);
84 return 1;
85 }
86
87 return execv(target, argv + 2);
88 }
89 EOT
90
91 [ -x "$1" ] || {
92 echo "compiling wrapper failed" >&2
93 exit 5
94 }
95 }
96
97 for LDD in ${PATH//://ldd }/ldd; do
98 "$LDD" --version >/dev/null 2>/dev/null && break
99 LDD=""
100 done
101
102 [ -n "$LDD" -a -x "$LDD" ] || LDD=
103
104 for BIN in "$@"; do
105 [ -n "$BIN" -a -n "$DIR" ] || {
106 echo "Usage: $0 <destdir> <executable> ..." >&2
107 exit 1
108 }
109
110 [ ! -d "$DIR/lib" ] && {
111 _md "$DIR/lib"
112 _md "$DIR/usr"
113 _ln "../lib" "$DIR/usr/lib"
114 }
115
116 [ ! -x "$DIR/lib/runas" ] && {
117 _wrapper "$DIR/lib/runas"
118 }
119
120 LDSO=""
121
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*)
125 case "$token" in
126 *ld-*.so*) LDSO="${token##*/}" ;;
127 esac
128
129 dest="$DIR/lib/${token##*/}"
130 ddir="${dest%/*}"
131
132 [ -f "$token" -a ! -f "$dest" ] && {
133 _md "$ddir"
134 _cp "$token" "$dest"
135 }
136 ;; esac
137 done
138 }
139
140 # is a dynamically linked executable
141 if [ -n "$LDSO" ]; then
142 echo "Bundling ${BIN##*/}"
143
144 RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}"
145 RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
146 REL="$(_relpath "$DIR/lib" "$BIN")"
147
148 _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin"
149
150 cat <<-EOF > "$BIN"
151 #!/usr/bin/env bash
152 dir="\$(dirname "\$0")"
153 exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/${REL:+$REL/}runas" "\$dir/.${BIN##*/}.bin" "\$0" "\$@"
154 EOF
155
156 chmod ${VERBOSE:+-v} 0755 "$BIN"
157 fi
158 done