summaryrefslogtreecommitdiff
path: root/other/docker/windows/build_toxcore.sh
blob: da465eb114cbb37a46f504281e7df436fe6d201c (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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/sh

set -e -x

#=== Cross-Compile Toxcore ===

build()
{
    ARCH=${1}

    echo "Building for ${ARCH} architecture"

    # set some things

    WINDOWS_TOOLCHAIN=${ARCH}-w64-mingw32

    # toxcore dependencies that we will copy to the user for static build of toxcore (e.g. vpx, opus, sodium)
    DEP_PREFIX_DIR="/root/prefix/${ARCH}"

    # toxcore dependencies that user doesn't need in build result
    EXTRA_DEP_PREFIX_DIR="/root/extra-prefix/${ARCH}"
    mkdir -p "${EXTRA_DEP_PREFIX_DIR}"

    # where to put the result of this particular build
    RESULT_PREFIX_DIR="/prefix/${ARCH}"
    mkdir -p "${RESULT_PREFIX_DIR}"

    # where to install static/shared toxcores before deciding whether they should be copied over to the user
    STATIC_TOXCORE_PREFIX_DIR="/tmp/static_prefix"
    SHARED_TOXCORE_PREFIX_DIR="/tmp/shared_prefix"
    mkdir -p "${STATIC_TOXCORE_PREFIX_DIR}" "${SHARED_TOXCORE_PREFIX_DIR}"

    export MAKEFLAGS=j$(nproc)
    export CFLAGS=-O3

    echo
    echo "=== Building toxcore ${ARCH} ==="
    export PKG_CONFIG_PATH="${DEP_PREFIX_DIR}/lib/pkgconfig:${EXTRA_DEP_PREFIX_DIR}/lib/pkgconfig"

    cp /toxcore /tmp/toxcore -R
    cd /tmp/toxcore/build
    echo "
        SET(CMAKE_SYSTEM_NAME Windows)

        SET(CMAKE_C_COMPILER   ${WINDOWS_TOOLCHAIN}-gcc)
        SET(CMAKE_CXX_COMPILER ${WINDOWS_TOOLCHAIN}-g++)
        SET(CMAKE_RC_COMPILER  ${WINDOWS_TOOLCHAIN}-windres)

        SET(CMAKE_FIND_ROOT_PATH /usr/${WINDOWS_TOOLCHAIN} ${DEP_PREFIX_DIR} ${EXTRA_DEP_PREFIX_DIR})
    " > windows_toolchain.cmake

    if [ "${ENABLE_TEST}" = "true" ]; then
        echo "SET(CROSSCOMPILING_EMULATOR /usr/bin/wine)" >> windows_toolchain.cmake
    fi

    cmake -DCMAKE_TOOLCHAIN_FILE=windows_toolchain.cmake \
        -DCMAKE_INSTALL_PREFIX="${STATIC_TOXCORE_PREFIX_DIR}" \
        -DENABLE_SHARED=OFF \
        -DENABLE_STATIC=ON \
        -DCMAKE_C_FLAGS="${CMAKE_C_FLAGS}" \
        -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS}" \
        -DCMAKE_EXE_LINKER_FLAGS="${CMAKE_EXE_LINKER_FLAGS}" \
        -DCMAKE_SHARED_LINKER_FLAGS="${CMAKE_SHARED_LINKER_FLAGS}" \
        ${EXTRA_CMAKE_FLAGS} \
        ..
    cmake --build . --target install -- -j$(nproc)

    if [ "${ENABLE_TEST}" = "true" ]; then
        rm -rf /root/.wine

        # setup wine
        if [ "${ARCH}" = "i686" ]; then
            export WINEARCH=win32
        else
            export WINEARCH=win64
        fi

        winecfg
        export CTEST_OUTPUT_ON_FAILURE=1
        # add libgcc_s_sjlj-1.dll libwinpthread-1.dll into PATH env var of wine
        export WINEPATH=`cd /usr/lib/gcc/${WINDOWS_TOOLCHAIN}/*posix/ ; winepath -w $(pwd)`\;`winepath -w /usr/${WINDOWS_TOOLCHAIN}/lib/`\;`winepath -w ${EXTRA_DEP_PREFIX_DIR}/bin`

        if [ "${ALLOW_TEST_FAILURE}" = "true" ]; then
            set +e
        fi
        cmake --build . --target test -- ARGS="-j50"
        if [ "${ALLOW_TEST_FAILURE}" = "true" ]; then
            set -e
        fi
    fi

    # move static dependencies
    cp "${STATIC_TOXCORE_PREFIX_DIR}"/* "${RESULT_PREFIX_DIR}" -R
    cp "${DEP_PREFIX_DIR}"/* "${RESULT_PREFIX_DIR}" -R

    # make libtox.dll
    cd "${SHARED_TOXCORE_PREFIX_DIR}"
    for archive in ${STATIC_TOXCORE_PREFIX_DIR}/lib/libtox*.a
    do
        ${WINDOWS_TOOLCHAIN}-ar xv ${archive}
    done
    ${WINDOWS_TOOLCHAIN}-gcc -Wl,--export-all-symbols \
                             -Wl,--out-implib=libtox.dll.a \
                             -shared \
                             -o libtox.dll \
                             *.obj \
                             ${STATIC_TOXCORE_PREFIX_DIR}/lib/*.a \
                             ${DEP_PREFIX_DIR}/lib/*.a \
                             /usr/${WINDOWS_TOOLCHAIN}/lib/libwinpthread.a \
                             -liphlpapi \
                             -lws2_32 \
                             -static-libgcc
    cp libtox.dll.a ${RESULT_PREFIX_DIR}/lib
    mkdir -p ${RESULT_PREFIX_DIR}/bin
    cp libtox.dll ${RESULT_PREFIX_DIR}/bin

    rm -rf /tmp/*

    # remove everything from include directory except tox headers
    mv ${RESULT_PREFIX_DIR}/include/tox ${RESULT_PREFIX_DIR}/tox
    rm -rf ${RESULT_PREFIX_DIR}/include/*
    mv ${RESULT_PREFIX_DIR}/tox ${RESULT_PREFIX_DIR}/include/tox

    sed -i "s|^prefix=.*|prefix=${RESULT_PREFIX_DIR}|g" ${RESULT_PREFIX_DIR}/lib/pkgconfig/*.pc
    sed -i "s|^libdir=.*|libdir=${RESULT_PREFIX_DIR}/lib|g" ${RESULT_PREFIX_DIR}/lib/*.la
}

#=== Test Supported vs. Enabled ===

if [ "${ENABLE_ARCH_i686}" != "true" ] && [ "${ENABLE_ARCH_x86_64}" != "true" ]; then
    echo "Error: No architecture specified. Set either ENABLE_ARCH_i686 or ENABLE_ARCH_x86_64 or both."
    exit 1
fi

if [ "${ENABLE_ARCH_i686}" = "true" ] && [ "${SUPPORT_ARCH_i686}" != "true" ]; then
    echo "Error: Can't build for i686 architecture because the image was created without SUPPORT_ARCH_i686 set"
    exit 1
fi

if [ "${ENABLE_ARCH_x86_64}" = "true" ] && [ "${SUPPORT_ARCH_x86_64}" != "true" ]; then
    echo "Error: Can't build for x86_64 architecture because the image was created without SUPPORT_ARCH_x86_64 set"
    exit 1
fi

if [ "${ENABLE_TEST}" = "true" ] && [ "${SUPPORT_TEST}" != "true" ]; then
    echo "Error: Can't build with tests because the image was created without SUPPORT_TEST set"
    exit 1
fi

#=== Build ===

if [ "${ENABLE_ARCH_i686}" = "true" ]; then
    build i686
fi

if [ "${ENABLE_ARCH_x86_64}" = "true" ]; then
    build x86_64
fi


tree -h /prefix

echo
echo "Built toxcore successfully!"
echo

# since we are building as root
chmod 777 /prefix -R