view darwin_files/standalone.py @ 3235:f53543bd5dd6 GSoC_end

[Darwin] Copy launch_octave.sh script inside the App Bundle
author Anirudha Bose <ani07nov@gmail.com>
date Tue, 24 Sep 2013 01:33:33 +0530
parents 2a5a191f6302
children
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:
## * Make sure the MXE directory is your current working directory. Create an App Bundle "octave.app"
##   and save it in the path "mxe-octave-anirudha/dist/"; then execute the following command:
##
##   python -mmacholib find ./dist/Octave-3.7.5.app/Contents/Resources/ 2>/dev/null | python ./darwin_files/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
import subprocess

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',''))
        print("[Completed]")
        Resources_dir = "./dist/Octave-3.7.5.app/Contents/Resources/"
        print("\nBuilding Octave Forge packages ...")
        os.system("make octave-forge-packages")
        os.system("cp ./build_packages.m %s/src/"%(Resources_dir))
        print("[Completed]")
        print("\nSetting up octaverc file ...")
        f = open("./dist/Octave-3.7.5.app/Contents/Resources/share/octave/site/m/startup/octaverc","w")
        f.write("EXEC_PATH (cstrcat (fullfile (OCTAVE_HOME, 'bin'), pathsep, EXEC_PATH));")
        f.close()
        print("[Completed]")
        print("\nMaking all files writable by user ...")
        os.system("chmod -R u+w %s"%(Resources_dir))
        print("[Completed]")
        print("\nCopying Octave Application Bundle launch script ...")
        os.system("cp ./darwin_files/launch_octave.sh ./dist/Octave-3.7.5.app/Contents/Resources/")
	os.system("chmod +x ./dist/Octave-3.7.5.app/Contents/Resources/launch_octave.sh")
        print("[Completed]")

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