Skip to content

Instantly share code, notes, and snippets.

@shotamatsuda
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shotamatsuda/43015c9d02f7cab1c828 to your computer and use it in GitHub Desktop.
Save shotamatsuda/43015c9d02f7cab1c828 to your computer and use it in GitHub Desktop.
Recursively bundle every linked library
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