| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copy a program to the specified destination, along |
| 4 | # with libraries it depends upon. |
| 5 | |
| 6 | src_bin=$1 |
| 7 | dest_dir=$2 |
| 8 | |
| 9 | # Returns true if the specified file is a dylib. |
| 10 | |
| 11 | is_dylib() { |
| 12 | case "$1" in |
| 13 | *.dylib) |
| 14 | true |
| 15 | ;; |
| 16 | *) |
| 17 | false |
| 18 | ;; |
| 19 | esac |
| 20 | } |
| 21 | |
| 22 | # Returns true if the specified file is in a system location |
| 23 | # (/System or /usr): |
| 24 | |
| 25 | is_sys_lib() { |
| 26 | case "$1" in |
| 27 | /System/*) |
| 28 | true |
| 29 | ;; |
| 30 | /usr/local/*) # homebrew/manual install |
| 31 | false |
| 32 | ;; |
| 33 | /usr/*) |
| 34 | true |
| 35 | ;; |
| 36 | *) |
| 37 | false |
| 38 | ;; |
| 39 | esac |
| 40 | } |
| 41 | |
| 42 | # Install the specified file to the location in dest_dir, along with |
| 43 | # any libraries it depends upon (recursively): |
| 44 | |
| 45 | install_with_deps() { |
| 46 | local src_file |
| 47 | local bin_name |
| 48 | local dest_file |
| 49 | local lib_name |
| 50 | |
| 51 | src_file=$1 |
| 52 | bin_name=$(basename "$src_file") |
| 53 | dest_file="$dest_dir/$bin_name" |
| 54 | |
| 55 | # Already copied into the package? Don't copy again. |
| 56 | # Prevents endless recursion. |
| 57 | |
| 58 | if [ -e "$dest_file" ]; then |
| 59 | return |
| 60 | fi |
| 61 | |
| 62 | echo "Installing $bin_name..." |
| 63 | |
| 64 | # Copy file into package. |
| 65 | |
| 66 | cp "$src_file" "$dest_file" |
| 67 | |
| 68 | # Copy libraries that this file depends on: |
| 69 | |
| 70 | otool -L "$src_file" | tail -n +2 | sed 's/^.//; s/ (.*//' | while read; do |
| 71 | # Don't copy system libraries |
| 72 | |
| 73 | if is_sys_lib "$REPLY"; then |
| 74 | continue |
| 75 | fi |
| 76 | |
| 77 | #echo " - $bin_name depends on $REPLY" |
| 78 | |
| 79 | # Copy this library into the package, and: |
| 80 | # recursively install any libraries that _this_ library depends on: |
| 81 | |
| 82 | install_with_deps "$REPLY" |
| 83 | |
| 84 | # Change destination binary to depend on the |
| 85 | # copy inside the package: |
| 86 | |
| 87 | lib_name=$(basename "$REPLY") |
| 88 | install_name_tool -change "$REPLY" "@executable_path/$lib_name" \ |
| 89 | "$dest_file" |
| 90 | done |
| 91 | |
| 92 | # If this is a library that we have installed, change its id: |
| 93 | |
| 94 | if is_dylib "$dest_file"; then |
| 95 | install_name_tool -id "@executable_path/$bin_name" "$dest_file" |
| 96 | fi |
| 97 | |
| 98 | # The install_name_tool calls above break signatures. Recent versions |
| 99 | # of macOS make signatures mandatory, even if just ad-hoc ones. |
| 100 | codesign --remove-signature "$dest_file" |
| 101 | codesign --force -s - "$dest_file" # ad-hoc signature |
| 102 | } |
| 103 | |
| 104 | # Install the file, and recursively install any libraries: |
| 105 | |
| 106 | install_with_deps "$src_bin" |
| 107 |