bundle-libraries.sh 2.7 KB

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