Skip to content

Instantly share code, notes, and snippets.

@drkibitz
Created February 9, 2014 08:11
Show Gist options
  • Save drkibitz/8896013 to your computer and use it in GitHub Desktop.
Save drkibitz/8896013 to your computer and use it in GitHub Desktop.
Recursively list paths of shared libraries required by given binary. Usage: `otool-ls-r /bin/bash`
#!/bin/bash
list=""
otool_list() {
local lib="$1"
local libs=""
if [[ "$list" == "${list/:$lib/}" ]]; then
echo "$lib"
list="$list:$lib"
libs=$(otool -L "$lib" | sed -n 's/^ \(.*\) (compatibility version.*$/\1/p')
for lib in $libs
do
otool_list "$lib"
done
fi
}
otool_list "$@"
@drkibitz
Copy link
Author

drkibitz commented Feb 9, 2014

Copy everything from list to current directory:

for lib in $(otool_list "$@")
do
    linkDirname="$PWD$(dirname "$lib")"
    linkBasename="$(basename "$lib")"
    mkdir -p "$linkDirname"
    cp "$lib" "$linkDirname/$linkBasename"
    echo ".$lib => $lib"
done

@cantino
Copy link

cantino commented Jul 6, 2018

Thanks for sharing this. It didn't work for me, but this did:

#!/usr/bin/env ruby

def otool(path)
  `otool -L "#{path}"`.split("\n").grep(/compatibility version/).map { |l| l.strip.scan(/(.*?) \(compatibility version/)[0][0] }
end

def recurse(starting_point, found = {})
  otool(starting_point).each do |shared_lib|
    if !found[shared_lib]
      found[shared_lib] = true
      recurse(shared_lib, found)
    end
  end

  found
end

found = recurse(ARGV[0])
puts found.keys.join("\n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment