Skip to content

Instantly share code, notes, and snippets.

@jiahuif
Created December 24, 2021 01:27
Show Gist options
  • Save jiahuif/be451a8adbd7b76e84e4e93e1904833d to your computer and use it in GitHub Desktop.
Save jiahuif/be451a8adbd7b76e84e4e93e1904833d to your computer and use it in GitHub Desktop.
FROM alpine AS alpine-full
RUN apk add gcc g++ libc-dev linux-headers zlib-dev
FROM debian AS builder
ENV LLVM_VERSION=13.0.0
COPY --from=alpine-full / /alpine
RUN apt-get update && apt-get install -y curl xz-utils cmake ninja-build python3
WORKDIR /tmp
RUN mkdir llvm llvm-src
RUN curl -L "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VERSION}/clang+llvm-${LLVM_VERSION}-x86_64-linux-gnu-ubuntu-20.04.tar.xz" | tar -xJ --strip-components=1 -C llvm
RUN curl -L "https://github.com/llvm/llvm-project/releases/download/llvmorg-${LLVM_VERSION}/llvm-project-${LLVM_VERSION}.src.tar.xz" | tar -xJ --strip-components=1 -C llvm-src
ENV CC=clang
ENV CXX=clang++
ENV LDFLAGS='-fuse-ld=lld'
WORKDIR /tmp/llvm-src
ENV PATH=/tmp/llvm/bin:/usr/local/bin:/usr/bin:/bin
RUN cmake -B./build-compiler-rt -H./compiler-rt/ -Wno-dev -G Ninja -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_C_FLAGS="-march=x86-64-v3 -fpic" -DCMAKE_CXX_FLAGS="-march=x86-64-v3 -fpic" \
-DCMAKE_EXE_LINKER_FLAGS_RELEASE_INIT="$LDFLAGS -fuse-ld=lld -march=x86-64-v3 -fpic -Wl,-z,stack-size=2097152" \
-DCMAKE_SYSROOT=/alpine \
-DCMAKE_C_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_CXX_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_ASM_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCOMPILER_RT_USE_BUILTINS_LIBRARY=ON \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DCOMPILER_RT_BUILD_ORC=OFF \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_BUILD_PROFILE=OFF \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_BUILD_MEMPROF=OFF \
-DCOMPILER_RT_INSTALL_PATH="/usr/local/lib/clang/${LLVM_VERSION}" \
-DCMAKE_AR=`which llvm-ar` \
-DCMAKE_RANLIB=`which llvm-randlib` \
-DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-alpine-linux-musl && \
cmake --build ./build-compiler-rt && \
DESTDIR=/alpine cmake --install ./build-compiler-rt && \
DESTDIR=/dest cmake --install ./build-compiler-rt && \
rm -rf ./build-compiler-rt
# use just-built compiler-rt
ENV LDFLAGS='-fuse-ld=lld --rtlib=compiler-rt'
RUN cmake -B./build-runtimes -S runtimes -Wno-dev -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_PIC=ON -DLLVM_ENABLE_ZLIB=ON \
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind" \
-DCMAKE_C_FLAGS="-march=x86-64-v3" -DCMAKE_CXX_FLAGS="-march=x86-64-v3" \
-DCMAKE_SYSROOT=/alpine \
-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
-DLIBCXX_ENABLE_SHARED=ON -DLIBCXX_ENABLE_STATIC=ON \
-DLIBUNWIND_USE_COMPILER_RT=ON \
-DLIBCXX_HAS_MUSL_LIBC=ON \
-DLIBCXX_HAS_ATOMIC_LIB=OFF \
-DLIBCXX_HAS_GCC_S_LIB=OFF \
-DLIBCXX_USE_COMPILER_RT=ON \
-DLIBCXXABI_USE_COMPILER_RT=ON \
-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
-DLIBCXX_CXX_ABI=libcxxabi \
-DLLVM_ENABLE_RTTI=ON \
-DLLVM_ENABLE_COMPILER_RT=ON \
-DLLVM_HOST_TRIPLE=x86_64-alpine-linux-musl \
-DLLVM_ENABLE_LLD=ON \
-DLLVM_ENABLE_LTO=Thin \
-DCMAKE_C_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_CXX_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_ASM_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-alpine-linux-musl \
-DCMAKE_AR=`which llvm-ar` \
-DCMAKE_RANLIB=`which llvm-randlib` && \
cmake --build ./build-runtimes && \
DESTDIR=/alpine cmake --install ./build-runtimes && \
DESTDIR=/dest cmake --install ./build-runtimes && \
rm -rf ./build-runtimes
RUN apt-get install -y make
# build our own zlib
ENV ZLIB_VERSION=1.2.11
RUN cd /tmp && curl -L "https://zlib.net/zlib-${ZLIB_VERSION}.tar.gz" | tar xz && \
cd zlib-* && \
export AR=llvm-ar && \
export LD=ld.lld && \
export CFLAGS='-O3 -flto=thin -fpic' && \
cmake -G Ninja -B ./build -S . \
-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON \
-DCMAKE_SYSROOT=/alpine \
-DCMAKE_C_COMPILER_TARGET=x86_64-alpine-linux-musl && \
cmake --build ./build && \
DESTDIR=/alpine cmake --install ./build && \
cd /tmp && rm -rf zlib-*
ENV LDFLAGS='-fuse-ld=lld --rtlib=compiler-rt --stdlib=libc++ -L/alpine/usr/local/lib'
ENV LD_LIBRARY_PATH=/alpine/usr/local/lib
# link musl libc to builder, so that we can run llvm-tblgen
RUN ln -s /alpine/lib/ld-musl-x86_64.so.1 /lib/ && \
ln -s /alpine/lib/libc.musl-x86_64.so.1 /lib/
RUN cmake -B./build -H./llvm/ -Wno-dev -G Ninja -DCMAKE_BUILD_TYPE=Release\
-DLLVM_ENABLE_PIC=ON -DLLVM_BUILD_DOCS=OFF -DLLVM_BUILD_EXAMPLES=OFF -DLLVM_ENABLE_ZLIB=ON \
-DCMAKE_CROSSCOMPILING=ON \
-DLLVM_ENABLE_PROJECTS="clang;lld" \
-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON \
-DCMAKE_SYSROOT=/alpine \
-DCMAKE_C_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_CXX_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_ASM_COMPILER_TARGET=x86_64-alpine-linux-musl \
-DCMAKE_AR=`which llvm-ar` \
-DCMAKE_C_FLAGS="-march=x86-64-v3" \
-DCMAKE_CXX_FLAGS="-march=x86-64-v3 -stdlib++-isystem /alpine/usr/local/include/c++/v1" \
-DCMAKE_EXE_LINKER_FLAGS_INIT="$LDFLAGS -fuse-ld=lld -march=x86-64-v3 -Wl,-z,stack-size=2097152" \
-DLLVM_HOST_TRIPLE=x86_64-linux-gnu \
-DLLVM_ENABLE_LLD=ON \
-DCLANG_DEFAULT_CXX_STDLIB=libc++ \
-DCLANG_DEFAULT_RTLIB=compiler-rt \
-DCLANG_DEFAULT_LINKER=lld \
-DCLANG_DEFAULT_UNWINDLIB=libunwind \
-DZLIB_LIBRARY_RELEASE=/alpine/usr/local/lib/libz.a \
-DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-alpine-linux-musl \
-DLLVM_ENABLE_LTO=Thin && \
cmake --build ./build && \
DESTDIR=/alpine cmake --install ./build && \
DESTDIR=/dest cmake --install ./build && \
rm -rf ./build
FROM alpine AS dest
RUN apk add musl-dev linux-headers
COPY --from=builder /dest/ /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment