Last active
April 2, 2024 09:54
-
-
Save philip-lamb/870f335117fbeb511de12b377c0743bf to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#! /bin/bash | |
# | |
# Script to collect macOS dependent dynamic libraries outside system paths. | |
# | |
# Author(s): Philip Lamb | |
# | |
# Get our location. | |
OURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
function usage { | |
echo "Usage: $(basename $0) [-v|--verbose] [--debug] path_to_dylib [path_to_dylib2 ...]" | |
echo "Where an @rpath is found, it will be substituted for the directory containing the first dylib specified." | |
exit 1 | |
} | |
# -e = exit on errors | |
set -e | |
# -x = debug | |
#set -x | |
# Parse parameters | |
while test $# -gt 0 | |
do | |
case "$1" in | |
--debug) DEBUG= | |
;; | |
--verbose) VERBOSE=1 | |
;; | |
--search-prefix) SEARCH_PREFIXES="$SEARCH_PREFIXES $2" | |
shift | |
;; | |
-v) VERBOSE=1 | |
;; | |
--*) echo "bad option $1" | |
usage | |
;; | |
*) TARGETS="$TARGETS $1" | |
;; | |
esac | |
shift | |
done | |
if [ -z "$TARGETS" ]; then | |
echo "No target dylib specified." >&2 | |
exit 1; | |
fi | |
rpath_root="$(dirname "$(realpath "$(echo -n $TARGETS | cut -d' ' -f1)")")" | |
[[ -z ${VERBOSE} ]] || echo "Using $rpath_root as @rpath root." | |
system_prefixes="/System/ /usr/lib/ /lib/" | |
for ((i = 1; i <= $(echo -n $TARGETS | wc -w) ; i++)); do | |
target=$(echo -n $TARGETS | cut -d' ' -f$i) | |
[[ -z ${VERBOSE} ]] || echo "Examining dependencies of $target" | |
deps_raw=$(otool -L $target | tail -n +3) | |
while IFS= read -r line; do | |
dep=$(echo -n $line | cut -d' ' -f1) | |
# Don't bother if it's a system lib. | |
for system_prefix in $system_prefixes | |
do | |
if [[ $dep == $system_prefix* ]]; then | |
[[ -z ${VERBOSE} ]] || echo " -- $dep -- SYSTEM LIB" | |
continue 2; | |
fi | |
done | |
# We didn't match any of the system prefixes, so it's a user lib | |
[[ -z ${VERBOSE} ]] || echo " -- $dep" | |
# Replace any '@rpath' with the target's path. | |
if [[ "$dep" =~ ^@rpath/.* ]]; then | |
if [[ -f "${dep/@rpath/$rpath_root}" ]]; then | |
dep="${dep/@rpath/$rpath_root}" | |
else | |
for search_prefix in $SEARCH_PREFIXES | |
do | |
if [[ -f "${dep/@rpath/$rpath_root/$search_prefix}" ]]; then | |
dep="${dep/@rpath/$rpath_root/$search_prefix}" | |
break | |
fi | |
done | |
fi | |
fi | |
# Is it already in the list of processed deps? | |
if ! [[ " $TARGETS " =~ " $dep " ]]; then | |
# Append to deps list if unique. | |
TARGETS="$TARGETS $dep" | |
fi | |
done <<< "$deps_raw" | |
done | |
echo $TARGETS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment