Last active
August 29, 2015 14:08
-
-
Save shotamatsuda/43015c9d02f7cab1c828 to your computer and use it in GitHub Desktop.
Recursively bundle every linked library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
readonly DESTINATION_DIR="@executable_path/../Frameworks" | |
follow_symlink() { | |
local file=$1 | |
local target | |
target=$(readlink "${file}") | |
if [[ "${target}" ]]; then | |
local directory | |
directory=$(dirname "${file}") | |
file=$(follow_symlink "${directory}/${target}") | |
fi | |
echo "${file}" | |
} | |
bundle_dependency() { | |
local file | |
file=$(follow_symlink "$1") | |
local skip_rewrite=$2 | |
local copy | |
copy=$(basename "${file}") | |
if [[ $skip_rewrite == true && -e "${copy}" ]]; then | |
return | |
fi | |
cp -f "${file}" "." || return | |
chmod 0755 "${copy}" || return | |
local install_name | |
install_name=$(otool -D "${file}" | grep -v ":$" | egrep -o "[^/]+$") | |
install_name_tool -id "${DESTINATION_DIR}/${install_name}" "${copy}" || return | |
if [[ "${copy}" != "${install_name}" ]]; then | |
ln -sf "${copy}" "${install_name}" || return | |
fi | |
resolve_dependencies "${copy}" true | |
} | |
resolve_dependencies() { | |
local file=$1 | |
local skip_rewrite=$2 | |
otool -L "${file}" | grep -o "/usr/local.*dylib" | while read; do | |
local dependancy | |
dependancy=$(basename ${REPLY}) | |
install_name_tool -change "${REPLY}" "${DESTINATION_DIR}/${dependancy}" "${file}" | |
bundle_dependency "${REPLY}" $skip_rewrite | |
done | |
} | |
pushd "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}" | |
resolve_dependencies "${BUILT_PRODUCTS_DIR}/${EXECUTABLE_PATH}" false | |
popd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment