pkgsrc "make replace" or pkg rolling-replace library errors

From Wikistix

Occasionally, due to dependency changes, I see library errors during or after running pkg_rolling-replace or make replace. One solution is to force rebuilding all packages. Another way is to rebuild a targeted set of packages which reference the problem library. This may be done by something like the following, in this case on Mac OS X, where the missing library is libffi.6.dylib, as seen in the errors:

dlopen(/Users/stix/pkg64/lib/gio/modules/libgsettingsgconfbackend.so, 1): Library not loaded: /Users/stix/pkg64/lib/libffi.6.dylib
  Referenced from: /Users/stix/pkg64/lib/gio/modules/libgsettingsgconfbackend.so
  Reason: image not found
Failed to load module: /Users/stix/pkg64/lib/gio/modules/libgsettingsgconfbackend.so

Use pkg_admin to set the rebuild flag on the packages referencing the missing library:

pkg_admin set rebuild=YES $( \
for i in /Users/stix/pkg64/lib/*.dylib; do
  otool -L "$i" |
    grep -q 'libffi\.6' && echo $i;
done |
xargs -n 1 -I {} sh -c "pkg_info -F {} | head -1" |
awk '{print $3}' | cut -f 1 -d : | sort -u)

You are now free to run pkg_rolling-replace to rebuild the flagged packages in the correct order.