ddnet/scripts/darwin_strip_rpath.py

44 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-12-02 14:22:26 +00:00
import argparse
import subprocess
import re
def split_cmds(lines):
2020-12-02 14:22:26 +00:00
cmds = []
current = []
load_cmd_regex = re.compile(r"^Load command \d+$")
for line in lines:
if load_cmd_regex.match(line):
cmds.append(current)
current = []
continue
2020-12-02 14:22:26 +00:00
current.append(line.strip())
2020-12-02 14:22:26 +00:00
return cmds[1:]
def main():
2020-12-02 14:22:26 +00:00
p = argparse.ArgumentParser(description="Strip LC_RPATH commands from executable")
2020-12-02 14:22:26 +00:00
p.add_argument('otool', help="Path to otool")
p.add_argument('install_name_tool', help="Path to install_name_tool")
p.add_argument('executable', metavar="EXECUTABLE", help="The executable to strip")
args = p.parse_args()
2020-12-02 14:22:26 +00:00
otool = args.otool
install_name_tool = args.install_name_tool
executable = args.executable
2020-12-02 14:22:26 +00:00
cmds = split_cmds(subprocess.check_output([otool, "-l", executable]).decode().splitlines())
lc_rpath_cmds = [cmd for cmd in cmds if cmd[0] == "cmd LC_RPATH"]
2020-12-02 14:22:26 +00:00
path_regex = re.compile(r"^path (.*) \(offset \d+\)$")
rpaths = {k[0] for k in [[path_regex.match(part).group(1) for part in cmd if path_regex.match(part)] for cmd in lc_rpath_cmds]}
2020-12-02 14:22:26 +00:00
print("Found paths:")
2020-12-02 14:22:26 +00:00
for path in rpaths:
print("\t" + path)
subprocess.check_call([install_name_tool, "-delete_rpath", path, executable])
if __name__ == '__main__':
2020-12-02 14:22:26 +00:00
main()