mac_rpath.py 2.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/python

import sys
import getopt
import os
import subprocess
import re


# List directory files
def list_files(source_dir):
  source_dir = os.path.abspath(source_dir)
 
  # Build file list
  filelist = []
  for file in os.listdir(source_dir):
    if os.path.isdir(os.path.join(source_dir, file)) ==  False:
      root, ext = os.path.splitext(file)
Ghislain MARY's avatar
Ghislain MARY committed
19
      if ext == '.dylib' or ext == '.so':
20 21 22 23 24
        filelist.append(file)
  return filelist

# List libraries used in a file
def list_libraries(file):
25 26
  print("Exec: /usr/bin/otool -L %s" % file)
  ret = subprocess.check_output(["/usr/bin/otool", "-L", file]).decode('utf-8')
27 28 29 30 31 32 33 34 35 36 37 38

  librarylist = []
  for a in ret.split("\n"):
    pop = re.search("^\s+(\S*).*$", a)
    if pop:
      librarylist.append(pop.group(1))
  return librarylist

# Change library id
def change_library_id(file, id):
  id = "@loader_path/" + id
  os.chmod(file, 0o755)
39
  print("%s: Change id %s" % (file, id))
40 41 42 43
  ret = subprocess.check_output(["/usr/bin/install_name_tool", "-id", id, file], stderr=subprocess.STDOUT)

# Change path to a library in a file
def change_library_path(file, old, new, path = ""):
44
  if len(path)> 0 and path[-1] != '/':
45 46 47
    path = path + "/"
  new = "@loader_path/" + path + new
  os.chmod(file, 0o755)
48
  print("%s: Replace %s -> %s" % (file, old, new))
49 50 51 52
  ret = subprocess.check_output(["/usr/bin/install_name_tool", "-change", old, new, file])

# Replace libraries used by a file
def replace_libraries(file, name, libraries, path = ""):
53 54
  print("---------------------------------------------")
  print("Replace libraries in %s" % file)
55 56 57
  change_library_id(file, name)
  librarylist = list_libraries(file)
  for lib in libraries:
58
    if lib != name:
59 60 61
      completelib = [s for s in librarylist if lib in s]
      if len(completelib) == 1:
         change_library_path(file, completelib[0], lib, path)
62
  print("---------------------------------------------")
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

def main(argv=None):
  if argv is None:
      argv = sys.argv
  if len(argv) > 1 and len(argv) <= 3:
    path = argv[1]
    file_list = list_files(path)
    if len(argv) <= 2:
      for file in file_list:
        replace_libraries(os.path.join(path, file), file, file_list)
    else:
      path2 = argv[2]
      if os.path.isdir(path2):
        file_list2 = list_files(path2)
        for file in file_list2:
          replace_libraries(os.path.join(path2, file), file, file_list, os.path.relpath(path, path2))
      else:
        file = argv[2]
        replace_libraries(file, os.path.basename(file), file_list, os.path.relpath(path, os.path.dirname(file)))


if __name__ == "__main__":
    sys.exit(main())