2
0

bundle-libraries.sh 3.3 KB

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