| 1 |
#!/usr/bin/env python2 |
| 2 |
|
| 3 |
# Script to list recursive dylib dependencies for binaries/dylibs passed as arguments. |
| 4 |
# Modified from https://github.com/mixxxdj/mixxx/blob/master/build/osx/otool.py. |
| 5 |
# |
| 6 |
# SPDX-FileCopyrightText: 2015 by Shanti <listaccount at revenant dot org> |
| 7 |
# SPDX-FileCopyrightText: 2015-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 8 |
# |
| 9 |
# SPDX-License-Identifier: BSD-3-Clause |
| 10 |
# |
| 11 |
|
| 12 |
import sys |
| 13 |
import os |
| 14 |
import re |
| 15 |
import argparse |
| 16 |
|
| 17 |
# --------------------------------------------------------------- |
| 18 |
|
| 19 |
def system(s): |
| 20 |
"wrap system() to give us feedback on what it's doing" |
| 21 |
"anything using this call should be fixed to use SCons's declarative style (once you figure that out, right nick?)" |
| 22 |
print s, |
| 23 |
sys.stdout.flush() # ignore line buffering. |
| 24 |
result = os.system(s) |
| 25 |
print |
| 26 |
return result |
| 27 |
|
| 28 |
# --------------------------------------------------------------- |
| 29 |
|
| 30 |
SYSTEM_FRAMEWORKS = ["/System/Library/Frameworks"] |
| 31 |
SYSTEM_LIBPATH = ["/usr/lib"] # anything else? |
| 32 |
|
| 33 |
# paths to libs that we should copy in |
| 34 |
LOCAL_FRAMEWORKS = [ |
| 35 |
os.path.expanduser("~/Library/Frameworks"), |
| 36 |
"/Library/Frameworks", |
| 37 |
"/Network/Library/Frameworks" |
| 38 |
] |
| 39 |
|
| 40 |
LOCAL_LIBPATH = filter(lambda x: |
| 41 |
os.path.isdir(x), |
| 42 |
["/usr/local/lib", |
| 43 |
"/opt/local/lib", |
| 44 |
"/sw/local/lib"] |
| 45 |
) |
| 46 |
|
| 47 |
# However |
| 48 |
FRAMEWORKS = LOCAL_FRAMEWORKS + SYSTEM_FRAMEWORKS |
| 49 |
LIBPATH = LOCAL_LIBPATH + SYSTEM_LIBPATH |
| 50 |
|
| 51 |
# --------------------------------------------------------------- |
| 52 |
|
| 53 |
# otool parsing |
| 54 |
def otool(binary): |
| 55 |
|
| 56 |
"return in a list of strings the OS X 'install names' of Mach-O binaries (dylibs and programs)" |
| 57 |
"Do not run this on object code archive (.a) files, it is not designed for that." |
| 58 |
#if not os.path.exists(binary): raise Exception("'%s' not found." % binary) |
| 59 |
|
| 60 |
if not type(binary) == str: raise ValueError("otool() requires a path (as a string)") |
| 61 |
|
| 62 |
stdin, stdout, stderr = os.popen3('otool -L "%s"' % binary) |
| 63 |
|
| 64 |
try: |
| 65 |
# discard the first line since it is just the name of the file or an error message (or if reading .as, the first item on the list) |
| 66 |
header = stdout.readline() |
| 67 |
|
| 68 |
if not binary+":\n" == header: |
| 69 |
|
| 70 |
# as far as I know otool -L only parses .dylibs and .a (but if it does anything else we should cover that case here) |
| 71 |
if header.startswith("Archive : "): |
| 72 |
raise Exception("'%s' an archive (.a) file." % binary) |
| 73 |
else: |
| 74 |
raise Exception(stderr.readline().strip()) |
| 75 |
|
| 76 |
def parse(l): |
| 77 |
return l[1:l.rfind("(")-1] |
| 78 |
|
| 79 |
return [parse(l[:-1]) for l in stdout] #[:-1] is for stripping the trailing \n |
| 80 |
|
| 81 |
finally: |
| 82 |
stdin.close() |
| 83 |
stdout.close() |
| 84 |
stderr.close() |
| 85 |
|
| 86 |
# --------------------------------------------------------------- |
| 87 |
|
| 88 |
def dependencies(binary): |
| 89 |
l = otool(binary) |
| 90 |
|
| 91 |
if os.path.basename(l[0]) == os.path.basename(binary): |
| 92 |
id = l.pop(0) |
| 93 |
#print "Removing -id field result %s from %s" % (id, binary) |
| 94 |
return l |
| 95 |
|
| 96 |
# --------------------------------------------------------------- |
| 97 |
|
| 98 |
def embed_dependencies(binary, |
| 99 |
# Defaults from |
| 100 |
# http://www.kernelthread.com/mac/osx/programming.html |
| 101 |
LOCAL = [os.path.expanduser("~/Library/Frameworks"), |
| 102 |
"/Library/Frameworks", |
| 103 |
"/Network/Library/Frameworks", |
| 104 |
"/usr/local/lib", |
| 105 |
"/opt/local/lib", |
| 106 |
"/sw/local/lib"], |
| 107 |
SYSTEM = ["/System/Library/Frameworks", |
| 108 |
"/Network/Library/Frameworks", |
| 109 |
"/usr/lib"]): |
| 110 |
"LOCAL: a list of paths to consider to be for installed libs that must therefore be bundled. Override this if you are not getting the right libs." |
| 111 |
"SYSTEM: a list of system library search paths that should be searched for system libs but should not be recursed into; this is needed. Override this if you want to bunde the system libs for some reason." #XXX it's useful to expose LOCAL but is this really needed? |
| 112 |
"this is a badly named function" |
| 113 |
"Note: sometimes Mach-O binaries depend on themselves. Deal with it." |
| 114 |
|
| 115 |
# "ignore_missing means whether to ignore if we can't load a binary for examination |
| 116 |
# (e.g. if you have references to plugins) XXX is the list" |
| 117 |
|
| 118 |
#binary = os.path.abspath(binary) |
| 119 |
|
| 120 |
todo = dependencies(binary) |
| 121 |
done = [] |
| 122 |
orig = [] |
| 123 |
|
| 124 |
# This code can be factored. |
| 125 |
|
| 126 |
while todo: |
| 127 |
|
| 128 |
# because of how this is written, popping from the end is a depth-first search. |
| 129 |
# Popping from the front would be a breadth-first search. Neat! |
| 130 |
|
| 131 |
e = todo.pop() |
| 132 |
|
| 133 |
# Figure out the absolute path to the library |
| 134 |
|
| 135 |
if e.startswith('/'): |
| 136 |
p = e |
| 137 |
elif e.startswith('@'): |
| 138 |
# it's a relative load path |
| 139 |
raise Exception("Unable to make heads nor tails, sah, of install name '%s'. Relative paths are for already-bundled binaries, this function does not support them." % e) |
| 140 |
else: |
| 141 |
# experiments show that giving an unspecified path is asking dyld(1) to find the library for us. This covers that case. |
| 142 |
|
| 143 |
for P in ['']+LOCAL+SYSTEM: |
| 144 |
p = os.path.abspath(os.path.join(P, e)) |
| 145 |
#print "SEARCHING IN LIBPATH; TRYING", p |
| 146 |
|
| 147 |
if os.path.exists(p): |
| 148 |
break |
| 149 |
else: |
| 150 |
p = e # fallthrough to the exception below #XXX icky bad logic, there must be a way to avoid saying exists() twice |
| 151 |
|
| 152 |
# if not os.path.exists(p): |
| 153 |
# raise Exception("Dependent library '%s' not found. Make sure it is installed." % e) |
| 154 |
|
| 155 |
if not any(p.startswith(P) for P in SYSTEM): |
| 156 |
|
| 157 |
if ".framework/Versions/" in p: |
| 158 |
# If dependency is a framework, return |
| 159 |
# framework directory not shared library |
| 160 |
|
| 161 |
f = re.sub("/Versions/[0-9]*/.*","",p) |
| 162 |
|
| 163 |
if f not in done: |
| 164 |
done.append(f) |
| 165 |
todo.extend(dependencies(p)) |
| 166 |
orig.append(e) |
| 167 |
elif p not in done: |
| 168 |
done.append(p) |
| 169 |
todo.extend(dependencies(p)) |
| 170 |
orig.append(e) |
| 171 |
|
| 172 |
assert all(e.startswith("/") for e in done), "embed_dependencies() is broken, some path in this list is not absolute: %s" % (done,) |
| 173 |
|
| 174 |
return sorted(done) |
| 175 |
|
| 176 |
# --------------------------------------------------------------- |
| 177 |
|
| 178 |
deplist = [] |
| 179 |
|
| 180 |
for dep in sys.argv[1:]: |
| 181 |
deplist = deplist + embed_dependencies(dep) |
| 182 |
|
| 183 |
depset = set(deplist) |
| 184 |
|
| 185 |
for dep in depset: |
| 186 |
print dep |