summary refs log tree commit diff
path: root/pkgs/development/compilers/ios-cross-compile/9.2_builder.sh
blob: 4eef8acb069343ac82884a35371236f2ce3fc4b2 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- shell-script -*-
if [ -e "$NIX_ATTRS_SH_FILE" ]; then . "$NIX_ATTRS_SH_FILE"; elif [ -f .attrs.sh ]; then . .attrs.sh; fi
source $stdenv/setup

function extract
{
    printf "extracting $(basename $1) ...\n"
    local tarflags="xf"

    case "$1" in
        *.tar.xz)
        xz -dc $1 | tar "$tarflags" - ;;
    *)
        printf "Make sure you give a iPhoneOS9.2.sdk.tar.xz file \n" ;;
    esac
}

function verify_arch {
    case "$1" in
    # Our good arches.
    armv7|arm64) ;;
    *)
        local
        acc="armv7 | arm64"
        error_message=$(
        printf '%s is not an acceptable arch. Try one of %s' "$1" "$acc"
             )
        printf "$error_message\n"
        exit
    esac
}

function verify_sdk_version {
    sdk_version=$(basename "$1" | grep -P -o "[0-9].[0-9]+")
    case "$sdk_version" in
    # Make sure the SDK is correct.
    [5-9].[0-9]) ;;
    *)
        printf 'No iPhone SDK version in file name\n'
    esac
}

function do_build {

    if [ $# -lt 2 ]; then
    printf "usage: $0 iPhoneOS.sdk.tar* <target cpu>\n" 1>&2
    printf "i.e. $0 /path/to/iPhoneOS.sdk.tar.xz armv7\n" 1>&2
    exit 1
    fi

    mkdir -p $out

    chmod -R 755 "$cctools_port"

    pushd "$cctools_port"/usage_examples/ios_toolchain &> /dev/null

    export LC_ALL=C

    local
    triple='%s-apple-darwin11'
    target_dir="$PWD/target"
    sdk_dir="$target_dir/SDK"
    platform="$(uname -s)"
    # Will be mutated by verify_sdk_version
    sdk_version=

    mkdir -p "$target_dir"
    mkdir -p "$target_dir/bin"
    mkdir -p "$sdk_dir"

    verify_arch "$2"
    verify_sdk_version "$1"

    triple="$(printf "$triple" "$2")"
    pushd "$sdk_dir" &>/dev/null
    extract "$1"

    local sys_lib=$(
        find $sdk_dir -name libSystem.dylib -o -name libSystem.tbd | head -n1
          )

    if [ -z "$sys_lib" ]; then
        printf "SDK should contain libSystem{.dylib,.tbd}\n" 1>&2
        exit 1
    fi

    local sys_root=$(readlink -f "$(dirname $sys_lib)/../..")

    local sdk_unboxed=$(basename $sys_root)

    mv -f "$sys_root"/* "$sdk_dir" || true

    popd &>/dev/null

    printf "\nbuilding wrapper\n"

    printf "int main(){return 0;}" | clang -xc -O2 -o "$target_dir"/bin/dsymutil -

    clang -O2 -std=c99 $alt_wrapper \
          -DTARGET_CPU=$(printf '"%s"' "$2") \
          -DNIX_APPLE_HDRS=$(
        printf '"%s"' "-I$out/$sdk/usr/include"
          ) \
          -DNIX_APPLE_FRAMEWORKS=$(
        printf '"%s"' "$out/$sdk/System/Library/Frameworks"
          ) \
          -DNIX_APPLE_PRIV_FRAMEWORKS=$(
        printf '"%s"' "$out/$sdk/System/Library/PrivateFrameworks"
          ) \
          -DOS_VER_MIN=$(printf '"%s"' "7.1") \
          -o "$target_dir/bin/$triple-clang"

    pushd "$target_dir"/bin &>/dev/null

    cp "$triple"-clang "$triple"-clang++

    popd &>/dev/null

    printf "\nbuilding ldid\n"

    mkdir -p tmp
    pushd tmp &>/dev/null
    pushd "$ldid" &>/dev/null

    chmod -R 755 "$ldid"

    make INSTALLPREFIX="$target_dir" -j4 install
    popd &>/dev/null
    popd &>/dev/null

    printf "\nbuilding cctools / ld64\n"

    pushd ../../cctools &>/dev/null
    git clean -fdx . &>/dev/null || true
    ./autogen.sh
    ./configure --target="$triple" --prefix="$target_dir"
    make -j4
    make install &>/dev/null

    popd &>/dev/null

    local me=`whoami`

    for d in bin libexec SDK; do
        chown -R $me:$me target/$d
        cp -R target/$d $out
    done

    # Crucial piece
    rm -rf $out/$sdk
    mv $out/SDK $out/$sdk
}

do_build $src armv7