view darwin_files/standalone.py @ 3222:b25bac22a278

darwin_files/standalone.py: Add information about the assumed location of MXE directory
author Anirudha Bose <ani07nov@gmail.com>
date Sun, 22 Sep 2013 00:50:43 +0530
parents df242658d93d
children 8654ba32cd42
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] 2>/dev/null | python standalone.py
##
## Example:
## * python -mmacholib find ./Octave-3.7.5.app/Contents/Resources/ 2>/dev/null | 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.
## * The script assumes that the path to MXE directory starts with "/Users...". For example, the MXE
##   directory can be located in a place like "/Users/mac6-user1/mxe/mxe-octave-anirudha/".
##   The Mach-O headers which point to System libraries or libraries belonging to Macports are
##   ignored.

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):
    m = MachO(path)
    os.system("sudo strip "+path+" 2>/dev/null")
    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 = 'sudo install_name_tool -change "%s" "@executable_path/../%s" ./%s'%(other,relative_name,path)
                        os.system(command)
                elif other.endswith(".so") and not other.startswith("@executable_path"):
                        command = 'sudo install_name_tool -change "%s" "@executable_path/../lib/%s" ./%s'%(other,other,path)
                        os.system(command)
def main():
        print("Stripping Mach-O files and rewriting Mach-O headers ...")
        for path in sys.stdin:
                dylibs_fix(path.replace('\n',''))

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