summaryrefslogtreecommitdiff
path: root/scripts/osx_fixbundle.sh
blob: a1f21de16ae3b64bce3164dc86fffd96282985fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# v 1.1 Supports migration of links (limited to the same directory) - I should add checks..
# usage osx_fixbundle.sh <bundle-name> <bzr_root>

if [[ ! -f version.h ]]; then 
    echo "**"
    echo "** ERROR: $0 doesn't seems to be launched from the kicad's bzr root !!!"
    echo "** Go in the bzr root directory and launch: scripts/osx_fixbundle.sh"
    echo "**"
    exit 1
fi

EXECUTABLES="`find . -name '*.app'`"

#
# Copies libraries under <bzr_root> in the bundle and relocates them in the binary
#

function fixbundle() {
    exec="$1"
    bzroot="$2"
    execpath="$3"
    binary="$4"
  
    LIBRARIES="`otool -L ${binary} | cut -d' ' -f1`"
    
    for library in $LIBRARIES; do
        
        mkdir -p ${execpath}${exec}.app/Contents/Frameworks
        if [[ "$library" =~ "$bzroot" ]]; then
            echo "${exec}: Migrating `basename $library` in the bundle"
            if [ ! -f ${exec}.app/Contents/Frameworks/`basename $library` ]; then
                if [ ! -L $library ]; then
                    cp -f $library ${execpath}${exec}.app/Contents/Frameworks
                else
                    resolvelink "$library" "`dirname $library`" "${execpath}/${exec}.app/Contents/Frameworks"
                fi
            fi
            install_name_tool -change $library @executable_path/../Frameworks/`basename $library` ${binary}
        fi
    done

    # Resolve issue in python modules (.so)
    cd ${execpath}
    MODULES="`find ${exec}.app -name '*.so'`"
    
    for module in $MODULES; do
        LIBRARIES="`otool -L $module | cut -d' ' -f1`"
        mkdir -p ${exec}.app/Contents/Frameworks

        for library in $LIBRARIES; do
            if [[ "$library" =~ "$bzroot" ]]; then
                if [ ! -f ${exec}.app/Contents/Frameworks/`basename $library` ]; then
                    if [ ! -L $library ]; then
                        cp -f $library ${exec}.app/Contents/Frameworks
                    else
                        resolvelink "$library" "`dirname $library`" "${execpath}/${exec}.app/Contents/Frameworks"
                    fi
                fi
                install_name_tool -change $library @executable_path/../Frameworks/`basename $library` $module
            fi 
        done
        echo "${exec}: elaborated module `basename ${module}`"
    done

    # Resolve issue between DYNLIBS
    dynlib_migrate="1";
    dynlib_cycle="0";
    
    while [ $dynlib_migrate -gt 0 ]; do
        dynlib_migrate="0";
        (( dynlib_cycle += 1 ))
        DYNLIBS="`find ${exec}.app -name '*.dylib'`"
    
        for dynlib in $DYNLIBS; do
            LIBRARIES="`otool -L $dynlib | cut -d' ' -f1`"
            mkdir -p ${exec}.app/Contents/Frameworks
    
            for library in $LIBRARIES; do
                if [[ "$library" =~ "$bzroot" ]]; then
                    if [ ! -f ${exec}.app/Contents/Frameworks/`basename $library` ]; then
                        if [ ! -L $library ]; then
                            cp -f $library ${exec}.app/Contents/Frameworks
                        else
                            resolvelink "$library" "`dirname $library`" "${execpath}/${exec}.app/Contents/Frameworks"
                        fi
                        echo "copied `basename $library` into bundle"
                        (( dynlib_migrate += 1))
                    fi

                    install_name_tool -change $library @executable_path/../Frameworks/`basename $library` $dynlib
                fi
            done
        done
        echo "${exec}: bundle dynlib dependencies migration Pass $dynlib_cycle: Migrated $dynlib_migrate libraries in bundle"
    done  
    cd - >/dev/null
}

#
# This supports only links on the same dir (TODO ?)
#

function resolvelink() {
    local srclib="`basename $1`"
    local srcpath="$2"
    local destpath="$3"

    #if is a file i expect a pointed ""
    local pointed="`readlink ${srcpath}/${srclib}`"
    
    if [ ! -f ${pointed} ]; then
        resolvelink "${pointed}" "${srcpath}" "${destpath}"
        echo "Link ${srclib} -> ${pointed} "
        (cd  "${destpath}"; ln -s "${pointed}" "${srclib}" )
    else
        echo "Copy ${srcpath}/${srclib} -> ${destpath} "
        cp "${srcpath}/${srclib}" ${destpath}
    fi
}

for executable in $EXECUTABLES;
do
   myexecpath="`dirname ${executable}`/"
   myexec="`basename ${executable}|sed -e 's/\.app//'`"

   fixbundle "${myexec}" "$1" "${myexecpath}" "${myexecpath}${myexec}.app/Contents/MacOS/${myexec}"
   fixbundle "${myexec}" "$1" "${myexecpath}" "${myexecpath}${myexec}.app/Contents/MacOS/_${myexec}.kiface"
done