view darwin_files/standalone.py @ 3217:a3a6870253d7

[Darwin] darwin_files/standalone.py: Enable running of script on the same application bundle more than once
author Anirudha Bose <ani07nov@gmail.com>
date Sat, 21 Sep 2013 16:52:09 +0530
parents 488d0100db6e
children de181f6edcbf
line wrap: on
line source

#!/usr/bin/env python
##
## This Python script can be used to rewrite dylib references in Mach-O headers
## and resolve the dynamic shared library install names which originally point
## to the libraries in MXE $(HOST_LIBDIR).
##
## Requirements:
## * Python 2.7
## * Macholib (pip install http://pypi.python.org/pypi/macholib)
##
## Usage:
## * python -mmacholib find [path to Resources directory of .app] | python standalone.py
##
## Example:
## * python -mmacholib find ./Octave-3.7.5.app/Contents/Resources/ | python standalone.py
##
## Important Notes:
## * Octave should be compiled with the option -headerpad_max_install_names in the LDFLAGS.
## * The script must be executed as root, if Octave was compiled with root permissions.
## * Before applying this script, make sure the bin and lib directory is present inside the
##   Resources folder.
## * Shared libraries like libcholmod.so, libumfpack.so, and others which end with .so
##   extension are not assigned any path in the Mach-O headers during compilation. This is
##   likely to happen while using MXE. This script can fix such errors too.

from __future__ import print_function
from macholib.MachO import MachO

__author__ = "Anirudha Bose"
__email__ = "ani07nov@gmail.com"

import os
import sys

def dylibs_fix(path):
    print(path)
    m = MachO(path)
    for header in m.headers:
        seen = set()
        for idx, name, other in header.walkRelocatables():
            if other not in seen:
                seen.add(other)
                relative_name = other[other.find("lib"):]
                if other.startswith("/Users"):
                        command = 'install_name_tool -change "%s" "@executable_path/../%s" ./%s\n'%(other,relative_name,path)
                        print(command)
                        os.system(command)
                elif other.endswith(".so") and not other.startswith("@executable_path"):
                        command = 'install_name_tool -change "%s" "@executable_path/../lib/%s" ./%s\n'%(other,other,path)
                        print(command)
                        os.system(command)
def main():
        for path in sys.stdin:
                dylibs_fix(path.replace('\n',''))

if __name__ == '__main__':
    try:
        sys.exit(main())
    except KeyboardInterrupt:
        pass