# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
# SPDX-FileCopyrightText: 2019-2022 Second State INC

FROM quay.io/pypa/manylinux2014_aarch64

MAINTAINER hydai hydai@secondstate.io

ADD SHA256SUM /root/

ENV PATH /opt/rh/devtoolset-10/root/usr/bin${PATH:+:${PATH}}
ENV MANPATH /opt/rh/devtoolset-10/root/usr/share/man${MANPATH:+:${MANPATH}}
ENV INFOPATH /opt/rh/devtoolset-10/root/usr/share/info${INFOPATH:+:${INFOPATH}}
ENV PKG_CONFIG_PATH /opt/rh/devtoolset-10/root/usr/lib64/pkgconfig${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}

RUN cd && (yum check-update || true) && yum install -y xz openssl-devel rpm-build centos-release-scl && \
    yum install -y devtoolset-10 && \
    export CPU=$(/opt/python/cp311-cp311/bin/python3 -c \
        'import multiprocessing; print(multiprocessing.cpu_count())') && \
    export CFGFLAGS="--prefix=/opt/rh/devtoolset-10/root/usr --disable-shared --libdir=/opt/rh/devtoolset-10/root/usr/lib64" && \
    curl -s -L -O --remote-name-all \
        https://github.com/facebook/zstd/releases/download/v1.5.5/zstd-1.5.5.tar.gz \
        https://github.com/Kitware/CMake/releases/download/v3.26.4/cmake-3.26.4.tar.gz \
        https://github.com/ninja-build/ninja/archive/refs/tags/v1.11.1.tar.gz \
        https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.5/llvm-16.0.5.src.tar.xz \
        https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.5/lld-16.0.5.src.tar.xz \
        https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.5/libunwind-16.0.5.src.tar.xz \
        https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.5/cmake-16.0.5.src.tar.xz \
        https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.5/third-party-16.0.5.src.tar.xz \
        https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.5/clang-16.0.5.src.tar.xz && \
    sha256sum -c SHA256SUM && \
    gzip -dc zstd-1.5.5.tar.gz | tar -xf - && \
    gzip -dc cmake-3.26.4.tar.gz | tar -xf - && \
    gzip -dc v1.11.1.tar.gz | tar -xf - && \
    xz -dc llvm-16.0.5.src.tar.xz | tar -xf - && \
    xz -dc lld-16.0.5.src.tar.xz | tar -xf - && \
    xz -dc libunwind-16.0.5.src.tar.xz | tar -xf - && \
    xz -dc cmake-16.0.5.src.tar.xz | tar -xf - && \
    xz -dc third-party-16.0.5.src.tar.xz | tar -xf - && \
    xz -dc clang-16.0.5.src.tar.xz | tar -xf - && \
    export ZSTDFLAGS=(PREFIX=/opt/rh/devtoolset-10/root/usr LIBDIR=/opt/rh/devtoolset-10/root/usr/lib64 SED_ERE_OPT=--regexp-extended MOREFLAGS="-std=c17 -O3 -fPIC -fPIE -fvisibility=hidden") && \
    cd zstd-1.5.5 && make -s "${ZSTDFLAGS[@]}" -j $CPU && make -s "${ZSTDFLAGS[@]}" install && rm -vf /opt/rh/devtoolset-10/root/usr/lib64/libzstd.so* && cd - && \
    mkdir build && cd build && /opt/python/cp311-cp311/bin/python \
        ../ninja-1.11.1/configure.py --bootstrap \
        --with-python=/opt/python/cp311-cp311/bin/python && \
    cp -v ninja /opt/rh/devtoolset-10/root/usr/bin/ninja && cd - && rm -rf build && \
    mkdir build && cd build && ../cmake-3.26.4/configure --prefix=/opt/rh/devtoolset-10/root/usr \
        --parallel=$CPU && make -s -j $CPU && make -s install && cd - && rm -rf build && \
    mv -v llvm-16.0.5.src llvm && \
    mv -v lld-16.0.5.src lld && \
    mv -v libunwind-16.0.5.src libunwind && \
    mv -v cmake-16.0.5.src cmake && \
    mv -v third-party-16.0.5.src third-party && \
    mv -v clang-16.0.5.src clang && \
    cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_INSTALL_PREFIX=/opt/rh/devtoolset-10/root/usr \
        -DPython3_ROOT_DIR=/opt/python/cp311-cp311 -DLLVM_LIBDIR_SUFFIX=64 \
        -DLLVM_TARGETS_TO_BUILD="AArch64;BPF" -DLLVM_ENABLE_PROJECTS="lld;clang" \
        -DLLVM_DEFAULT_TARGET_TRIPLE="aarch64-redhat-linux-gnu" \
        -DBUILD_SHARED_LIBS=OFF llvm && \
    cmake --build build --target install && \
    rm -rf build && rm -rf *

RUN yum clean all
