Skip to content

Instantly share code, notes, and snippets.

@mohd-akram
Last active April 19, 2025 13:47
Show Gist options
  • Save mohd-akram/3b8b4a75cc8fafd59aef00e5e39a4488 to your computer and use it in GitHub Desktop.
Save mohd-akram/3b8b4a75cc8fafd59aef00e5e39a4488 to your computer and use it in GitHub Desktop.
Compare the dependencies listed by a MacPorts port with the output of otool
#!/bin/sh
set -euo pipefail
usage() {
printf >&2 "usage: %s [-a] [portname]\n" "$(basename "$0")"
}
archive=
verbose=
while getopts :av opt; do
case $opt in
a) archive=1 ;;
v) verbose=1 ;;
?) usage; exit 2
esac
done
shift $((OPTIND-1))
if ! [ "${1-}" ]; then
name=$(basename "$PWD")
else
case $1 in
subport=*) name=${1#subport=} ;;
*) name=$1
esac
fi
if [ "$archive" ]; then
base=https://packages.macports.org
dir=$base/$name
file=$(curl -fsSL "$dir/" | grep -o '"[^"]*\.tbz2"' | tr -d \" |
sort -V | tail -n1)
url=$dir/$file
d=/tmp/mparchives/$(basename "$file" .tbz2)
if ! [ -d "$d" ]; then
mkdir -p "$d.tmp"
curl -fsSL "$url" | tar -C "$d.tmp" -xf -
mv "$d.tmp" "$d"
fi
contents=$(find "$d" -type f)
else
contents=$(port -q contents "$name")
fi
get_libs() {
while read -r f
do if [ -x "$f" ] && ! [ -L "$f" ]; then printf "%s\n" "$f"; fi
done | sed -e 's/"/"\\""/g' -e 's/.*/"&"/' | xargs -E '' otool -L |
awk '/^[[:space:]]/{print $1}' | sort | uniq
}
libs=$(printf '%s\n' "$contents" | get_libs)
{
# portfile deps
port -q deps --no-build ${1-} |
awk -F': ' '/Lib/{gsub(/, /,"\n");print $NF}'
echo
# actual deps
printf '%s\n' "$libs" |
sed -e 's/"/"\\""/g' -e 's/.*/"&"/' | xargs -E '' port -q provides |
awk -v name="$name" '$1!=name{print $1}' | sort | uniq
} | awk '
/^$/{ c = 1; next }
{ if(c) b[$1] = 1; else a[$1] = 1 }
END {
for (p in a) if (!b[p]) print "- " p
for (p in b) if (!a[p]) print "+ " p
}
' | sort -k2,2 || {
printf '%s: failed to get diff\n' "$(basename "$0")"
exit 1
}
if [ "$verbose" ] && [ "$libs" ]; then
printf >&2 '\nLibraries:\n%s\n' "$libs"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment