bundle-libraries.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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" ] || LDD=
  44. for BIN in "$@"; do
  45. [ -n "$BIN" -a -x "$BIN" -a -n "$DIR" ] || {
  46. echo "Usage: $0 <destdir> <executable> ..." >&2
  47. exit 1
  48. }
  49. [ ! -d "$DIR/bundled/lib" ] && {
  50. _md "$DIR/bundled/lib"
  51. _md "$DIR/bundled/usr"
  52. _ln "../lib" "$DIR/bundled/usr/lib"
  53. }
  54. LDSO=""
  55. echo "Bundling ${BIN##*/}"
  56. [ -n "$LDD" ] && {
  57. for token in $("$LDD" "$BIN" 2>/dev/null); do
  58. case "$token" in */*.so*)
  59. case "$token" in
  60. *ld-*.so*) LDSO="${token##*/}" ;;
  61. *) echo " * lib: ${token##*/}" ;;
  62. esac
  63. dest="$DIR/bundled/lib/${token##*/}"
  64. ddir="${dest%/*}"
  65. [ -f "$token" -a ! -f "$dest" ] && {
  66. _md "$ddir"
  67. _cp "$token" "$dest"
  68. }
  69. ;; esac
  70. done
  71. }
  72. _md "$DIR"
  73. # is a dynamically linked executable
  74. if [ -n "$LDSO" ]; then
  75. _cp "$BIN" "$DIR/bundled/${BIN##*/}"
  76. RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
  77. [ -x "$DIR/bundled/$RUN" ] || {
  78. cat <<-EOF > "$DIR/bundled/$RUN"
  79. #!/usr/bin/env bash
  80. dir="\$(dirname "\$0")"
  81. bin="\$(basename "\$0")"
  82. exec -a "\$0" "\$dir/bundled/lib/$LDSO" --library-path "\$dir/bundled/lib" "\$dir/bundled/\$bin" "\$@"
  83. EOF
  84. chmod ${VERBOSE:+-v} 0755 "$DIR/bundled/$RUN"
  85. }
  86. _ln "./bundled/$RUN" "$DIR/${BIN##*/}"
  87. # is a static executable or non-elf binary
  88. else
  89. [ -n "$LDD" ] && echo " * not dynamically linked"
  90. _cp "$BIN" "$DIR/${BIN##*/}"
  91. fi
  92. done