bundle-libraries.sh 2.6 KB

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