FROM rippleci/clio_clang:16
ARG DEBIAN_FRONTEND=noninteractive
ARG TARGETARCH

SHELL ["/bin/bash", "-c"]
USER root
WORKDIR /root

ENV CCACHE_VERSION=4.10.2 \
    LLVM_TOOLS_VERSION=19 \
    GH_VERSION=2.40.0 \
    DOXYGEN_VERSION=1.12.0

# Add repositories
RUN apt-get -qq update \
    && apt-get -qq install -y --no-install-recommends --no-install-suggests gnupg wget curl software-properties-common \
    && echo "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-${LLVM_TOOLS_VERSION} main" >> /etc/apt/sources.list \
    && wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add -

# Install packages
RUN apt update -qq \
    && apt install -y --no-install-recommends --no-install-suggests python3 python3-pip git git-lfs make ninja-build flex bison jq graphviz \
    clang-tidy-${LLVM_TOOLS_VERSION} clang-tools-${LLVM_TOOLS_VERSION} \
    && pip3 install -q --upgrade --no-cache-dir pip && pip3 install -q --no-cache-dir conan==1.62 gcovr cmake==3.31.6 pre-commit \
    && apt-get clean && apt remove -y software-properties-common

# Install gcc-12 and make ldconfig aware of the new libstdc++ location (for gcc)
# Note: Clang is using libc++ instead
COPY --from=rippleci/clio_gcc:12.3.0 /gcc12.deb /
RUN apt update && apt-get install -y binutils libc6-dev \
    && dpkg -i /gcc12.deb \
    && rm -rf /gcc12.deb \
    && ldconfig

# Rewire to use gcc-12 as default compiler
RUN update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 100 \
    && update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++-12 100 \
    && update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100 \
    && update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-12 100 \
    && update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-12 100 \
    && update-alternatives --install /usr/bin/gcov-dump gcov-dump /usr/bin/gcov-dump-12 100 \
    && update-alternatives --install /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-12 100

WORKDIR /tmp

# Install ccache from source
RUN wget "https://github.com/ccache/ccache/releases/download/v${CCACHE_VERSION}/ccache-${CCACHE_VERSION}.tar.gz" \
    && tar xf "ccache-${CCACHE_VERSION}.tar.gz" \
    && cd "ccache-${CCACHE_VERSION}" \
    && mkdir build && cd build \
    && cmake -GNinja -DCMAKE_BUILD_TYPE=Release .. \
    && cmake --build . --target install \
    && rm -rf /tmp/* /var/tmp/*

# Install doxygen from source
RUN wget "https://github.com/doxygen/doxygen/releases/download/Release_${DOXYGEN_VERSION//./_}/doxygen-${DOXYGEN_VERSION}.src.tar.gz" \
    && tar xf "doxygen-${DOXYGEN_VERSION}.src.tar.gz" \
    && cd "doxygen-${DOXYGEN_VERSION}" \
    && mkdir build && cd build \
    && cmake -GNinja -DCMAKE_BUILD_TYPE=Release .. \
    && cmake --build . --target install \
    && rm -rf /tmp/* /var/tmp/*

# Install gh
RUN wget https://github.com/cli/cli/releases/download/v${GH_VERSION}/gh_${GH_VERSION}_linux_${TARGETARCH}.tar.gz \
    && tar xf gh_${GH_VERSION}_linux_${TARGETARCH}.tar.gz \
    && mv gh_${GH_VERSION}_linux_${TARGETARCH}/bin/gh /usr/bin/gh \
    && rm -rf /tmp/* /var/tmp/*

WORKDIR /root
# Using root by default is not very secure but github checkout action doesn't work with any other user
# https://github.com/actions/checkout/issues/956
# And Github Actions doc recommends using root
# https://docs.github.com/en/actions/sharing-automations/creating-actions/dockerfile-support-for-github-actions#user

# Setup conan
RUN conan remote add --insert 0 conan-non-prod http://18.143.149.228:8081/artifactory/api/conan/conan-non-prod

# Note: intentionally leaving cppstd=20
RUN conan profile new gcc --detect \
    && conan profile update settings.compiler=gcc gcc \
    && conan profile update settings.compiler.version=12 gcc \
    && conan profile update settings.compiler.cppstd=20 gcc \
    && conan profile update settings.compiler.libcxx=libstdc++11 gcc \
    && conan profile update env.CC=/usr/bin/gcc-12 gcc \
    && conan profile update env.CXX=/usr/bin/g++-12 gcc \
    && conan profile update "conf.tools.build:compiler_executables={\"c\": \"/usr/bin/gcc-12\", \"cpp\": \"/usr/bin/g++-12\"}" gcc

RUN conan profile new clang --detect \
    && conan profile update settings.compiler=clang clang \
    && conan profile update settings.compiler.version=16 clang \
    && conan profile update settings.compiler.cppstd=20 clang \
    && conan profile update settings.compiler.libcxx=libc++ clang \
    && conan profile update env.CC=/usr/bin/clang-16 clang \
    && conan profile update env.CXX=/usr/bin/clang++-16 clang \
    && conan profile update env.CXXFLAGS="-DBOOST_ASIO_DISABLE_CONCEPTS" clang \
    && conan profile update "conf.tools.build:compiler_executables={\"c\": \"/usr/bin/clang-16\", \"cpp\": \"/usr/bin/clang++-16\"}" clang

RUN echo "include(gcc)" >> .conan/profiles/default

COPY conan/gcc.asan /root/.conan/profiles
COPY conan/gcc.tsan /root/.conan/profiles
COPY conan/gcc.ubsan /root/.conan/profiles
COPY conan/clang.asan /root/.conan/profiles
COPY conan/clang.tsan /root/.conan/profiles
COPY conan/clang.ubsan /root/.conan/profiles
