2021-04-16 00:59:52 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
sys_re = re.compile("^/System")
|
|
|
|
usr_re = re.compile("^/usr/lib/")
|
|
|
|
exe_re = re.compile("@executable_path")
|
|
|
|
|
|
|
|
def is_user_lib(objfile, libname):
|
|
|
|
return not sys_re.match(libname) and \
|
|
|
|
not usr_re.match(libname) and \
|
|
|
|
not exe_re.match(libname) and \
|
2014-07-15 08:37:39 +00:00
|
|
|
not "libobjc." in libname and \
|
|
|
|
not "libSystem." in libname and \
|
|
|
|
not "libc." in libname and \
|
|
|
|
not "libgcc." in libname and \
|
2015-02-02 15:11:11 +00:00
|
|
|
not os.path.basename(libname) == 'Python' and \
|
2019-04-22 10:49:02 +00:00
|
|
|
not os.path.basename(objfile) in libname and \
|
|
|
|
not "libswift" in libname
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
def otool(objfile):
|
2019-09-21 22:32:40 +00:00
|
|
|
command = "otool -L '%s' | grep -e '\t' | awk '{ print $1 }'" % objfile
|
2018-03-06 19:48:15 +00:00
|
|
|
output = subprocess.check_output(command, shell = True, universal_newlines=True)
|
|
|
|
return set(filter(partial(is_user_lib, objfile), output.split()))
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
def get_rpaths_dev_tools(binary):
|
|
|
|
command = "otool -l '%s' | grep -A2 LC_RPATH | grep path | grep \"Xcode\|CommandLineTools\"" % binary
|
|
|
|
result = subprocess.check_output(command, shell = True, universal_newlines=True)
|
|
|
|
pathRe = re.compile("^\s*path (.*) \(offset \d*\)$")
|
|
|
|
output = []
|
|
|
|
|
|
|
|
for line in result.splitlines():
|
|
|
|
output.append(pathRe.search(line).group(1).strip())
|
|
|
|
|
|
|
|
return output
|
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
def install_name_tool_change(old, new, objfile):
|
|
|
|
subprocess.call(["install_name_tool", "-change", old, new, objfile])
|
|
|
|
|
|
|
|
def install_name_tool_id(name, objfile):
|
|
|
|
subprocess.call(["install_name_tool", "-id", name, objfile])
|
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
def install_name_tool_add_rpath(rpath, binary):
|
|
|
|
subprocess.call(["install_name_tool", "-add_rpath", rpath, binary])
|
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
def install_name_tool_delete_rpath(rpath, binary):
|
|
|
|
subprocess.call(["install_name_tool", "-delete_rpath", rpath, binary])
|
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
def libraries(objfile, result = dict()):
|
|
|
|
libs_list = otool(objfile)
|
2018-03-06 19:48:15 +00:00
|
|
|
result[objfile] = libs_list
|
2014-04-26 21:49:42 +00:00
|
|
|
|
|
|
|
for lib in libs_list:
|
|
|
|
if lib not in result:
|
|
|
|
libraries(lib, result)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
def lib_path(binary):
|
|
|
|
return os.path.join(os.path.dirname(binary), 'lib')
|
|
|
|
|
|
|
|
def lib_name(lib):
|
|
|
|
return os.path.join("@executable_path", "lib", os.path.basename(lib))
|
|
|
|
|
2019-12-09 00:38:27 +00:00
|
|
|
def process_libraries(libs_dict, binary):
|
|
|
|
libs_set = set(libs_dict)
|
2022-05-12 15:12:31 +00:00
|
|
|
# Remove binary from libs_set to prevent a duplicate of the binary being
|
2019-12-09 00:38:27 +00:00
|
|
|
# added to the libs directory.
|
|
|
|
libs_set.remove(binary)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-12-09 00:38:27 +00:00
|
|
|
for src in libs_set:
|
2014-04-26 21:49:42 +00:00
|
|
|
name = lib_name(src)
|
|
|
|
dst = os.path.join(lib_path(binary), os.path.basename(src))
|
|
|
|
|
|
|
|
shutil.copy(src, dst)
|
|
|
|
os.chmod(dst, 0o755)
|
|
|
|
install_name_tool_id(name, dst)
|
|
|
|
|
|
|
|
if src in libs_dict[binary]:
|
|
|
|
install_name_tool_change(src, name, binary)
|
|
|
|
|
2019-12-09 00:38:27 +00:00
|
|
|
for p in libs_set:
|
2014-04-26 21:49:42 +00:00
|
|
|
if p in libs_dict[src]:
|
|
|
|
install_name_tool_change(p, lib_name(p), dst)
|
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
def process_swift_libraries(binary):
|
|
|
|
command = ['xcrun', '--find', 'swift-stdlib-tool']
|
|
|
|
swiftStdlibTool = subprocess.check_output(command, universal_newlines=True).strip()
|
2019-09-20 13:14:59 +00:00
|
|
|
# from xcode11 on the dynamic swift libs reside in a separate directory from
|
|
|
|
# the std one, might need versioned paths for future swift versions
|
|
|
|
swiftLibPath = os.path.join(swiftStdlibTool, '../../lib/swift-5.0/macosx')
|
|
|
|
swiftLibPath = os.path.abspath(swiftLibPath)
|
2019-04-22 10:49:02 +00:00
|
|
|
|
|
|
|
command = [swiftStdlibTool, '--copy', '--platform', 'macosx', '--scan-executable', binary, '--destination', lib_path(binary)]
|
2019-09-20 13:14:59 +00:00
|
|
|
|
|
|
|
if os.path.exists(swiftLibPath):
|
|
|
|
command.extend(['--source-libraries', swiftLibPath])
|
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
subprocess.check_output(command, universal_newlines=True)
|
|
|
|
|
|
|
|
print(">> setting additional rpath for swift libraries")
|
|
|
|
install_name_tool_add_rpath("@executable_path/lib", binary)
|
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
def remove_dev_tools_rapths(binary):
|
|
|
|
for path in get_rpaths_dev_tools(binary):
|
|
|
|
install_name_tool_delete_rpath(path, binary)
|
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
def main():
|
|
|
|
binary = os.path.abspath(sys.argv[1])
|
|
|
|
if not os.path.exists(lib_path(binary)):
|
|
|
|
os.makedirs(lib_path(binary))
|
2019-04-22 10:49:02 +00:00
|
|
|
print(">> gathering all linked libraries")
|
2014-04-26 21:49:42 +00:00
|
|
|
libs = libraries(binary)
|
2018-03-06 19:48:15 +00:00
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
print(">> copying and processing all linked libraries")
|
2019-12-09 00:38:27 +00:00
|
|
|
process_libraries(libs, binary)
|
2014-04-26 21:49:42 +00:00
|
|
|
|
2019-09-21 22:32:40 +00:00
|
|
|
print(">> removing rpath definitions towards dev tools")
|
|
|
|
remove_dev_tools_rapths(binary)
|
|
|
|
|
2019-04-22 10:49:02 +00:00
|
|
|
print(">> copying and processing swift libraries")
|
|
|
|
process_swift_libraries(binary)
|
|
|
|
|
2014-04-26 21:49:42 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|