fix-install-names.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/python
  2. import hashlib
  3. import optparse
  4. import ConfigParser
  5. import os
  6. import platform
  7. import subprocess
  8. import sys
  9. import shutil
  10. exts = (".dylib", ".so")
  11. exes = ("fc-cache", "macdeployqt", "qmake", "moc", "rcc", "qmlimportscanner", "QtWebEngineProcess")
  12. def exec_cmd(args, env={}, supress_output=False):
  13. cmd = subprocess.Popen(args, stdout = subprocess.PIPE, stderr = subprocess.STDOUT, env = env)
  14. output = ''
  15. while True:
  16. out = cmd.stdout.read(1)
  17. if out == '' and cmd.poll() != None:
  18. break
  19. if out != '':
  20. if not supress_output:
  21. sys.stdout.write(out)
  22. output += out
  23. if cmd.wait() != 0:
  24. raise Exception("Command failed: \"%s\"" % " ".join(args), output)
  25. return output
  26. def fix_install_name(path):
  27. for root, dirs, files in os.walk(path):
  28. for f in files:
  29. fpath = os.path.join(root, f)
  30. if os.path.basename(f) == "QtWebEngineProcess":
  31. print "-- Adding rpath (%s) to %s" % (os.path.join(path, "lib"), f)
  32. exec_cmd(["install_name_tool", "-add_rpath", os.path.join(path, "lib"), fpath], supress_output=True)
  33. if (f.endswith(exts) or os.path.basename(f) in exes or (".framework/Versions/" in root and os.access(fpath, os.X_OK))) and not os.path.islink(fpath) and os.path.exists(fpath):
  34. # Fix permissions
  35. if not os.access(fpath, os.W_OK) or not os.access(fpath, os.R_OK) or not os.access(fpath, os.X_OK):
  36. os.chmod(fpath, 0o644)
  37. try:
  38. basename = os.path.basename(fpath)
  39. otoolout = exec_cmd(["otool", "-L", fpath], supress_output=True)
  40. for l in otoolout.split("\n")[1:]:
  41. l = l.rstrip().strip()
  42. # See if we need to fix it up.
  43. if len(l) > 0 and (l.startswith("/Volumes/CI-OSX/") or (l[0] != '/' and not l.startswith("@rpath"))):
  44. current_lib = l.split(" (compat")[0]
  45. current_basename = os.path.basename(current_lib)
  46. correct_lib = os.path.join(root, current_basename)
  47. if ".framework" in current_lib:
  48. current_basename = "/".join(current_lib.split("/")[-4:])
  49. if not os.path.exists(correct_lib):
  50. # look for it further up, like in the root path:
  51. if os.path.exists(os.path.join(path, "lib", current_basename)):
  52. correct_lib = os.path.join(path, "lib", current_basename)
  53. elif os.path.exists(os.path.join(path, "lib", current_lib)):
  54. correct_lib = os.path.join(path, "lib", current_lib)
  55. else:
  56. print "Can't link %s" % current_lib
  57. continue
  58. # print current_lib, correct_lib
  59. if current_lib != correct_lib:
  60. if current_basename.split('.')[0] == basename.split('.')[0]:
  61. print "-- Fixing ID for", basename
  62. exec_cmd(["install_name_tool", "-id", correct_lib, fpath], supress_output=True)
  63. else:
  64. print "-- Fixing library link for %s (%s)" % (basename, current_basename)
  65. exec_cmd(["install_name_tool", "-change", current_lib, correct_lib, fpath], supress_output=True)
  66. except:
  67. print "** Fail when running installname on %s" % f
  68. raise
  69. continue
  70. if __name__=='__main__':
  71. if os.path.isdir(sys.argv[1]):
  72. fix_install_name(sys.argv[1])