osx-relocate-binary.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/bash
  2. set -e
  3. [ -z "$3" ] && echo "usage: \"$0\" <file> <source-prefix> <destination-dir>" && echo "This utility relocates binary <file> from <source-prefix> to <destonation-dir> together with all dependencies and adds rpath support." && exit 1
  4. PREFIX="$2"
  5. PREFIXLEN=${#PREFIX}
  6. DEST="$3"
  7. process_lib() {
  8. FILESRC=$1
  9. if [[ "${FILESRC}" == ${PREFIX}* ]]; then
  10. #FILEDEST_SHORT=`echo "${FILESRC}" | cut -c1-$PREFIXLEN`
  11. local FILEDEST_SHORT="${FILESRC:$PREFIXLEN}"
  12. local FILEDEST="${DEST}/${FILEDEST_SHORT}"
  13. if [ ! -f "${FILEDEST}" ]; then
  14. if [ ! -d `dirname "${FILEDEST}"` ]; then
  15. mkdir -p `dirname "${FILEDEST}"`
  16. fi
  17. cp "${FILESRC}" "${FILEDEST}"
  18. chmod a+rw "$FILEDEST"
  19. install_name_tool -add_rpath ./ "$FILEDEST" > /dev/null 2>&1 || true
  20. echo "Relinking ${FILEDEST_SHORT} ..."
  21. local FIRST=true
  22. local LINE=
  23. local LINE2=
  24. otool -L "${FILEDEST}" | while read -r LINE; do
  25. if $FIRST; then
  26. FIRST=false
  27. else
  28. LINE=`echo "$LINE" | sed -e 's/^[ \t]*//' | sed -e 's/ \(.*\)$//'`
  29. # make sure file isn't referencing itself
  30. #A=$(basename "$FILEDEST")
  31. #B=$(basename "$LINE")
  32. if [ ! "$LINE" == "$FILESRC" ] && [[ "$LINE" == $PREFIX* ]]; then
  33. #LINE2=`echo "${LINE}" | cut -c1-$PREFIXLEN`
  34. LINE2="${LINE:$PREFIXLEN}"
  35. install_name_tool -change "$LINE" "@rpath/$LINE2" "$FILEDEST"
  36. process_lib "$LINE"
  37. fi
  38. fi
  39. done
  40. #echo "... finished ${FILEDEST_SHORT}"
  41. fi
  42. fi
  43. }
  44. echo "Gathering deps for: $1"
  45. #scan "$BASE_FILE"
  46. #scan "$BASE_FILE" subscan
  47. process_lib "$1"
  48. echo "Success."
  49. echo ""