build-libcxx.sh 8.12 KB
Newer Older
1 2 3 4
#!/bin/sh

set -e

5 6 7 8 9 10
BUILD_STATIC=1
BUILD_SHARED=1

while [ $# -gt 0 ]; do
    if [ "$1" = "--disable-shared" ]; then
        BUILD_SHARED=
11 12
    elif [ "$1" = "--enable-shared" ]; then
        BUILD_SHARED=1
13 14
    elif [ "$1" = "--disable-static" ]; then
        BUILD_STATIC=
15 16
    elif [ "$1" = "--enable-static" ]; then
        BUILD_STATIC=1
17 18 19 20 21 22 23
    else
        PREFIX="$1"
    fi
    shift
done
if [ -z "$PREFIX" ]; then
    echo $0 [--disable-shared] [--disable-static] dest
24 25
    exit 1
fi
26 27 28 29

mkdir -p "$PREFIX"
PREFIX="$(cd "$PREFIX" && pwd)"

30 31
export PATH=$PREFIX/bin:$PATH

32 33
: ${CORES:=$(nproc 2>/dev/null)}
: ${CORES:=$(sysctl -n hw.ncpu 2>/dev/null)}
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
: ${CORES:=4}
: ${ARCHS:=${TOOLCHAIN_ARCHS-i686 x86_64 armv7 aarch64}}

if [ ! -d libunwind ]; then
    git clone -b master https://github.com/llvm-mirror/libunwind.git
    CHECKOUT_LIBUNWIND=1
fi
if [ ! -d libcxxabi ]; then
    git clone -b master https://github.com/llvm-mirror/libcxxabi.git
    CHECKOUT_LIBCXXABI=1
fi
if [ ! -d libcxx ]; then
    git clone -b master https://github.com/llvm-mirror/libcxx.git
    CHECKOUT_LIBCXX=1
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT_LIBUNWIND" ]; then
    cd libunwind
    [ -z "$SYNC" ] || git fetch
52
    git checkout 90c7a8cc28b3f0718e0726eeec32cc4ebb454c7e
53 54 55 56 57
    cd ..
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT_LIBCXXABI" ]; then
    cd libcxxabi
    [ -z "$SYNC" ] || git fetch
58
    git checkout 5f7425947f303be5ca9b1a6471471d92d209f498
59 60 61 62 63
    cd ..
fi
if [ -n "$SYNC" ] || [ -n "$CHECKOUT_LIBCXX" ]; then
    cd libcxx
    [ -z "$SYNC" ] || git fetch
64
    git checkout 8220dac54c22c42a5ec2a32a0ed50343a2ea4775
65 66 67 68 69
    cd ..
fi

LIBCXX=$(pwd)/libcxx

70 71 72 73 74 75 76 77
case $(uname) in
MINGW*)
    CMAKE_GENERATOR="MSYS Makefiles"
    ;;
*)
    ;;
esac

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
build_all() {
    type="$1"
    if [ "$type" = "shared" ]; then
        SHARED=TRUE
        STATIC=FALSE
    else
        SHARED=FALSE
        STATIC=TRUE
    fi

    cd libunwind
    for arch in $ARCHS; do
        mkdir -p build-$arch-$type
        cd build-$arch-$type
        # If llvm-config and the llvm cmake files are available, -w gets added
        # to the compiler flags; manually add it here to avoid noisy warnings
        # that normally are suppressed.
        cmake \
96
            ${CMAKE_GENERATOR+-G} "$CMAKE_GENERATOR" \
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
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX=$PREFIX/$arch-w64-mingw32 \
            -DCMAKE_C_COMPILER=$arch-w64-mingw32-clang \
            -DCMAKE_CXX_COMPILER=$arch-w64-mingw32-clang++ \
            -DCMAKE_CROSSCOMPILING=TRUE \
            -DCMAKE_SYSTEM_NAME=Windows \
            -DCMAKE_C_COMPILER_WORKS=TRUE \
            -DCMAKE_CXX_COMPILER_WORKS=TRUE \
            -DCMAKE_AR=$PREFIX/bin/llvm-ar \
            -DCMAKE_RANLIB=$PREFIX/bin/llvm-ranlib \
            -DLLVM_NO_OLD_LIBSTDCXX=TRUE \
            -DCXX_SUPPORTS_CXX11=TRUE \
            -DLIBUNWIND_USE_COMPILER_RT=TRUE \
            -DLIBUNWIND_ENABLE_THREADS=TRUE \
            -DLIBUNWIND_ENABLE_SHARED=$SHARED \
            -DLIBUNWIND_ENABLE_STATIC=$STATIC \
            -DLIBUNWIND_ENABLE_CROSS_UNWINDING=FALSE \
            -DLIBUNWIND_STANDALONE_BUILD=TRUE \
            -DCMAKE_CXX_FLAGS="-nostdinc++ -I$LIBCXX/include -w" \
            -DCMAKE_C_FLAGS="-w" \
            -DCMAKE_SHARED_LINKER_FLAGS="-lpsapi" \
            ..
        make -j$CORES
        make install
        if [ "$type" = "shared" ]; then
122
            mkdir -p $PREFIX/$arch-w64-mingw32/bin
123 124 125 126
            cp lib/libunwind.dll $PREFIX/$arch-w64-mingw32/bin
        else
            # Merge libpsapi.a into the static library libunwind.a, to
            # avoid having to specify -lpsapi when linking to it.
127
            llvm-ar qcsL \
128 129 130 131 132
                $PREFIX/$arch-w64-mingw32/lib/libunwind.a \
                $PREFIX/$arch-w64-mingw32/lib/libpsapi.a
        fi
        cd ..
    done
133 134
    cd ..

135 136 137 138 139 140 141 142 143 144 145 146 147
    cd libcxxabi
    for arch in $ARCHS; do
        mkdir -p build-$arch-$type
        cd build-$arch-$type
        # If llvm-config and the llvm cmake files are available, -w gets added
        # to the compiler flags; manually add it here to avoid noisy warnings
        # that normally are suppressed.
        if [ "$type" = "shared" ]; then
            LIBCXXABI_VISIBILITY_FLAGS="-D_LIBCPP_BUILDING_LIBRARY -U_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"
        else
            LIBCXXABI_VISIBILITY_FLAGS="-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"
        fi
        cmake \
148
            ${CMAKE_GENERATOR+-G} "$CMAKE_GENERATOR" \
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX=$PREFIX/$arch-w64-mingw32 \
            -DCMAKE_C_COMPILER=$arch-w64-mingw32-clang \
            -DCMAKE_CXX_COMPILER=$arch-w64-mingw32-clang++ \
            -DCMAKE_CROSSCOMPILING=TRUE \
            -DCMAKE_SYSTEM_NAME=Windows \
            -DCMAKE_C_COMPILER_WORKS=TRUE \
            -DCMAKE_CXX_COMPILER_WORKS=TRUE \
            -DCMAKE_AR=$PREFIX/bin/llvm-ar \
            -DCMAKE_RANLIB=$PREFIX/bin/llvm-ranlib \
            -DLIBCXXABI_USE_COMPILER_RT=ON \
            -DLIBCXXABI_ENABLE_EXCEPTIONS=ON \
            -DLIBCXXABI_ENABLE_THREADS=ON \
            -DLIBCXXABI_TARGET_TRIPLE=$arch-w64-mingw32 \
            -DLIBCXXABI_ENABLE_SHARED=OFF \
            -DLIBCXXABI_LIBCXX_INCLUDES=../../libcxx/include \
165
            -DLIBCXXABI_LIBDIR_SUFFIX="" \
166 167 168 169 170 171 172 173
            -DLIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS=OFF \
            -DLLVM_NO_OLD_LIBSTDCXX=TRUE \
            -DCXX_SUPPORTS_CXX11=TRUE \
            -DCMAKE_CXX_FLAGS="$LIBCXXABI_VISIBILITY_FLAGS -D_LIBCPP_HAS_THREAD_API_WIN32 -w" \
            ..
        make -j$CORES
        cd ..
    done
174 175
    cd ..

176 177 178 179 180 181 182 183 184 185
    cd libcxx
    for arch in $ARCHS; do
        mkdir -p build-$arch-$type
        cd build-$arch-$type
        if [ "$type" = "shared" ]; then
            LIBCXX_VISIBILITY_FLAGS="-D_LIBCXXABI_BUILDING_LIBRARY"
        else
            LIBCXX_VISIBILITY_FLAGS="-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"
        fi
        cmake \
186
            ${CMAKE_GENERATOR+-G} "$CMAKE_GENERATOR" \
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX=$PREFIX/$arch-w64-mingw32 \
            -DCMAKE_C_COMPILER=$arch-w64-mingw32-clang \
            -DCMAKE_CXX_COMPILER=$arch-w64-mingw32-clang++ \
            -DCMAKE_CROSSCOMPILING=TRUE \
            -DCMAKE_SYSTEM_NAME=Windows \
            -DCMAKE_C_COMPILER_WORKS=TRUE \
            -DCMAKE_CXX_COMPILER_WORKS=TRUE \
            -DCMAKE_AR=$PREFIX/bin/llvm-ar \
            -DCMAKE_RANLIB=$PREFIX/bin/llvm-ranlib \
            -DLIBCXX_USE_COMPILER_RT=ON \
            -DLIBCXX_INSTALL_HEADERS=ON \
            -DLIBCXX_ENABLE_EXCEPTIONS=ON \
            -DLIBCXX_ENABLE_THREADS=ON \
            -DLIBCXX_HAS_WIN32_THREAD_API=ON \
            -DLIBCXX_ENABLE_MONOTONIC_CLOCK=ON \
            -DLIBCXX_ENABLE_SHARED=$SHARED \
            -DLIBCXX_ENABLE_STATIC=$STATIC \
            -DLIBCXX_SUPPORTS_STD_EQ_CXX11_FLAG=TRUE \
            -DLIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB=TRUE \
            -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
            -DLIBCXX_ENABLE_FILESYSTEM=OFF \
            -DLIBCXX_ENABLE_STATIC_ABI_LIBRARY=TRUE \
            -DLIBCXX_CXX_ABI=libcxxabi \
            -DLIBCXX_CXX_ABI_INCLUDE_PATHS=../../libcxxabi/include \
            -DLIBCXX_CXX_ABI_LIBRARY_PATH=../../libcxxabi/build-$arch-$type/lib \
213
            -DLIBCXX_LIBDIR_SUFFIX="" \
214
            -DLIBCXX_INCLUDE_TESTS=FALSE \
215 216 217 218 219 220 221
            -DCMAKE_CXX_FLAGS="$LIBCXX_VISIBILITY_FLAGS" \
            -DCMAKE_SHARED_LINKER_FLAGS="-lunwind -Wl,--export-all-symbols" \
            -DLIBCXX_ENABLE_ABI_LINKER_SCRIPT=FALSE \
            ..
        make -j$CORES
        make install
        if [ "$type" = "shared" ]; then
222
            llvm-ar qcsL \
223 224 225 226
                $PREFIX/$arch-w64-mingw32/lib/libc++.dll.a \
                $PREFIX/$arch-w64-mingw32/lib/libunwind.dll.a
            cp lib/libc++.dll $PREFIX/$arch-w64-mingw32/bin
        else
227
            llvm-ar qcsL \
228 229 230 231 232
                $PREFIX/$arch-w64-mingw32/lib/libc++.a \
                $PREFIX/$arch-w64-mingw32/lib/libunwind.a
        fi
        cd ..
    done
233
    cd ..
234
}
235

236 237 238 239
# Build shared first and static afterwards; the headers for static linking also
# work when linking against the DLL, but not vice versa.
[ -z "$BUILD_SHARED" ] || build_all shared
[ -z "$BUILD_STATIC" ] || build_all static