FROM debian:trixie-slim
ARG JOBS_COUNT=12
ENV CROSS_COMPILE=x86_64-w64-mingw32-
ENV SYSROOT=/usr/lib/gcc/x86_64-w64-mingw32/14-posix

# locales
RUN apt-get update && apt-get install -y locales \
    && localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 \
    && rm -rf /var/cache/apt/archives/*
ENV LANG=en_US.utf8

# configs
COPY .bashrc /root/.bashrc
COPY inputrc /etc/inputrc

# base soft
RUN apt-get install -y subversion git zip unzip wget curl htop net-tools iputils-ping mc nano screen g++ patchelf p7zip-full ninja-build make python3 \
 cpio libglu1-mesa-dev libgl1-mesa-dev libwayland-dev libncurses-dev flex bison gperf libdbus-1-3 \
 ccache libffi-dev libssl-dev doxygen graphviz libx11-dev libxkbcommon-dev libpkgconfig-perl libfontconfig1-dev \
 liblzma-dev libxml2-dev libbz2-dev libmpc-dev libmpfr-dev libgmp-dev libicu-dev libusb-1.0-0-dev libreadline-dev \
 ninja-build mingw-w64 libz-mingw-w64-dev \
 && rm -rf /var/cache/apt/archives/*

# w64-mingw32 soft
RUN update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix \
 && update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix

RUN x86_64-w64-mingw32-strip -v --strip-unneeded /usr/x86_64-w64-mingw32/lib/*.dll && \
	x86_64-w64-mingw32-strip -v --strip-unneeded ${SYSROOT}/*.dll
	
WORKDIR /soft

COPY toolchain-Windows.cmake /soft/
COPY opencl_win.zip /soft/
COPY rpc.zip /soft/
COPY zlib1.dll /soft/windows/bin/


ENV CUR_SODIUM_VERSION=1.0.20
ENV CUR_FFTW_VERSION=3.3.8
ENV CUR_ASSIMP_VERSION=4.1.0
ENV CUR_DISCOUNT_VERSION=2.2.6
ENV CUR_OPENSSL_VERSION=1_1_1h
ENV CUR_OPENCV_VERSION=4.5.5


RUN mkdir -p /soft/windows/bin/ \
 && cp -v ${SYSROOT}/libgcc_s_seh-1.dll /soft/windows/bin/ \
 && cp -v ${SYSROOT}/libstdc++-6.dll /soft/windows/bin/ \
 && cp -v /usr/x86_64-w64-mingw32/lib/libwinpthread-1.dll /soft/windows/bin/
RUN unzip -o opencl_win.zip -d /soft/windows \
 && unzip -o rpc.zip -d /soft/windows/lib \
 && rm -vf /soft/opencl_win.zip /soft/rpc.zip

# last cmake from sources, into /opt
ENV CUR_CMAKE_VERSION=3.31.3
RUN wget -nv https://github.com/Kitware/CMake/releases/download/v${CUR_CMAKE_VERSION}/cmake-${CUR_CMAKE_VERSION}-Linux-x86_64.sh \
 && mkdir -p /opt/cmake \
 && bash ./cmake-${CUR_CMAKE_VERSION}-Linux-x86_64.sh --skip-license --prefix=/opt/cmake \
 && rm -rf /soft/cmake-${CUR_CMAKE_VERSION}-Linux-x86_64.sh

ENV PATH=/opt/cmake/bin:$PATH 


# Remove invalid FindGtest.cmake
RUN cd "$(find /opt/cmake/share -name FindGTest.cmake -type f -printf '%h' -quit)" && rm FindGTest.cmake


# sodium
WORKDIR /soft
RUN curl -s https://download.libsodium.org/libsodium/releases/libsodium-${CUR_SODIUM_VERSION}.tar.gz \
 -o libsodium-${CUR_SODIUM_VERSION}.tar.gz \
 && tar -xf libsodium-${CUR_SODIUM_VERSION}.tar.gz -C /soft \
 && rm -f libsodium-${CUR_SODIUM_VERSION}.tar.gz \
 && mkdir -p /soft/windows/build/sodium && cd /soft/windows/build/sodium \
 && sh -c 'CFLAGS="-O3 -fomit-frame-pointer -m64" LDFLAGS="-O3 -fomit-frame-pointer -m64" /soft/libsodium-${CUR_SODIUM_VERSION}/configure --host=x86_64-w64-mingw32 --prefix=/soft/windows/ --exec-prefix=/soft/windows && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/windows/build/sodium && rm -rf /soft/libsodium-*


# fftw3
WORKDIR /soft
RUN wget -nv http://www.fftw.org/fftw-${CUR_FFTW_VERSION}.tar.gz \
 && tar -xf fftw-${CUR_FFTW_VERSION}.tar.gz -C /soft \
 && rm -f fftw-${CUR_FFTW_VERSION}.tar.gz \
 && mkdir -p /soft/windows/build/fftw3 && cd /soft/windows/build/fftw3 \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_FLOAT=1 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=1 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && rm -rf /soft/fftw-*


# assimp
WORKDIR /soft
RUN wget -nv https://github.com/assimp/assimp/archive/v${CUR_ASSIMP_VERSION}.tar.gz \
 && tar -xf v${CUR_ASSIMP_VERSION}.tar.gz -C /soft \
 && rm -f v${CUR_ASSIMP_VERSION}.tar.gz pax_global_header \
 && mkdir -p /soft/windows/build/assimp && cd /soft/windows/build/assimp \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DASSIMP_BUILD_ASSIMP_TOOLS=0 -DASSIMP_BUILD_TESTS=0 -DBUILD_FRAMEWORK=0 -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake /soft/assimp-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/assimp-*


# markdown
WORKDIR /soft
RUN wget -nv https://github.com/Orc/discount/archive/v${CUR_DISCOUNT_VERSION}.tar.gz \
 && tar -xf v${CUR_DISCOUNT_VERSION}.tar.gz -C /soft \
 && rm -f v${CUR_DISCOUNT_VERSION}.tar.gz
RUN sed -i 's/DESTRUCTOR/ /' /soft/discount-${CUR_DISCOUNT_VERSION}/setup.c
WORKDIR /soft/discount-${CUR_DISCOUNT_VERSION}/cmake
COPY discount.cmake.patch /soft/discount-${CUR_DISCOUNT_VERSION}/cmake/cmake.patch
RUN patch < cmake.patch
WORKDIR /soft/linux/build/discount
RUN cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-${CUR_DISCOUNT_VERSION}/cmake && make -j2 && mv mktags .. && rm -rf ./* \
&& mkdir -p /soft/windows/build/discount && mv /soft/linux/build/mktags /soft/windows/build/discount/mktags
WORKDIR /soft/windows/build/discount
RUN cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-*/cmake \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/discount-*


# mysql
COPY MySQL_C_x64.zip /soft/
RUN unzip -o /soft/MySQL_C_x64.zip -d ${SYSROOT}/ \
 && cp ${SYSROOT}/libmysql.dll /soft/windows/bin/ \
 && rm /soft/MySQL_C_x64.zip


# postgresql
COPY postgresql.zip /soft/
RUN unzip -o /soft/postgresql.zip -d ${SYSROOT}/ \
 && cp ${SYSROOT}/lib/*.dll /soft/windows/bin/ \
 && rm /soft/postgresql.zip


# OpenSSL
RUN wget -nv https://github.com/openssl/openssl/archive/OpenSSL_${CUR_OPENSSL_VERSION}.tar.gz \
 && tar -xf OpenSSL_${CUR_OPENSSL_VERSION}.tar.gz -C /soft \
 && rm -f OpenSSL_${CUR_OPENSSL_VERSION}.tar.gz \
 && mkdir -p /soft/windows/build/openssl && cd /soft/windows/build/openssl \
 && /soft/openssl-OpenSSL_${CUR_OPENSSL_VERSION}/Configure --cross-compile-prefix=x86_64-w64-mingw32- \
	--prefix=/soft/windows/ --release no-idea no-mdc2 no-rc5 no-tests shared mingw64 \
 && make -j${JOBS_COUNT} && make install \
 && cd /soft && rm -rf /soft/windows/build/openssl && rm -rf /soft/openssl-OpenSSL_*

# microhttpd
RUN mkdir -p /soft/tmp && cd /soft/tmp \
 && wget https://ftpmirror.gnu.org/libmicrohttpd/libmicrohttpd-latest-w32-bin.zip \
 && unzip -o /soft/tmp/*.zip -d /soft/tmp \
 && cp -rfv /soft/tmp/libmicrohttpd-*-w32-bin/x86_64/MinGW/static/mingw64/* /soft/windows/ \
 && rm -rf /soft/tmp


# curl
RUN mkdir -p /soft/tmp && cd /soft/tmp \
 && wget https://curl.se/windows/latest.cgi?p=win64-mingw.zip \
 && unzip -o /soft/tmp/*.zip -d /soft/tmp \
 && cp -rv /soft/tmp/curl-*/bin /soft/tmp/curl-*/lib /soft/tmp/curl-*/include ${SYSROOT} \
 && cp -v ${SYSROOT}/bin/libcurl-x64.dll /soft/windows/bin/ \
 && cp -v ${SYSROOT}/lib/*curl.dll.a /soft/windows/lib/ \
 && rm -rf /soft/tmp


RUN chmod +r /soft/windows/bin/*

ENV BUILD_TYPE=Release

# GTest
WORKDIR /soft
RUN git clone https://github.com/google/googletest.git \
 && mkdir googletest/build && cd googletest/build \
 && cmake -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake .. \
 && make -j${JOBS_COUNT} && make install && cd /soft && rm -rf googletest

WORKDIR /soft
RUN git clone --depth 1 --recursive -b 'release/21.x' https://github.com/llvm/llvm-project.git

WORKDIR /soft/llvm_build_windows
RUN cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/soft/windows -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Windows.cmake \
-DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DLLVM_ENABLE_RUNTIMES="" -DLLVM_ENABLE_RTTI=ON \
-DLLVM_BUILD_TESTS=OFF -DLLVM_INCLUDE_TESTS=OFF -DLLVM_INSTALL_GTEST=OFF ../llvm-project/llvm \
 && cmake --build ./ --target install -j8 \
 && rm -rf *
