ARG DOCKER_PREFIX=wapmobil/
ARG DOCKER_BITS=
FROM ${DOCKER_PREFIX}windows-base-${DOCKER_BITS}

ENV WIN_TRIPPLET=${WIN_ARCH}-w64-mingw32
ENV CROSS_COMPILE=${WIN_TRIPPLET}-
ENV SYSROOT=/usr/lib/gcc/${WIN_TRIPPLET}/${MINGW_VERSION}-posix


# w64-mingw32 soft (prepare, strip)
RUN update-alternatives --set ${WIN_TRIPPLET}-g++ /usr/bin/${WIN_TRIPPLET}-g++-posix \
 && update-alternatives --set ${WIN_TRIPPLET}-gcc /usr/bin/${WIN_TRIPPLET}-gcc-posix

RUN ${WIN_TRIPPLET}-strip -v --strip-unneeded /usr/${WIN_TRIPPLET}/lib/*.dll && \
	${WIN_TRIPPLET}-strip -v --strip-unneeded ${SYSROOT}/*.dll

RUN mkdir -p ${INSTALL_DIR}/bin/ \
 && cp -v ${SYSROOT}/libgcc_*.dll ${INSTALL_DIR}/bin/ \
 && cp -v ${SYSROOT}/libstdc++-6.dll ${INSTALL_DIR}/bin/ \
 && cp -v /usr/${WIN_TRIPPLET}/lib/libwinpthread-1.dll ${INSTALL_DIR}/bin/


#
# Ready-to-use libraries and tools
# unpack only
#

WORKDIR /soft

# RPC
RUN unzip -o rpc_x${WIN_BITS}.zip -d ${INSTALL_DIR}/lib

# fxc
RUN unzip -o /soft/fxc2.zip -d /soft/ \
 && echo -e "#! /bin/sh\nwine /soft/fxc2/fxc.exe \$*" > /usr/sbin/fxc.exe \
 && chmod +x /usr/sbin/fxc.exe

# OpenCL
RUN unzip -o opencl_win_x${WIN_BITS}.zip -d ${INSTALL_DIR}/

# mysql
RUN unzip -o /soft/MySQL_C_x${WIN_BITS}.zip -d ${SYSROOT}/ \
 && cp ${SYSROOT}/libmysql.dll ${INSTALL_DIR}/bin/

# postgresql
RUN unzip -o /soft/postgresql_x${WIN_BITS}.zip -d ${SYSROOT}/ \
 && cp ${SYSROOT}/lib/*.dll ${INSTALL_DIR}/bin/

# 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/${WIN_ARCH2}/MinGW/static/mingw${WIN_BITS}/* ${INSTALL_DIR}/ \
 && rm -rf /soft/tmp

# curl
#RUN git clone -b curl-${CUR_CURL_VERSION} --depth 1 https://github.com/curl/curl \
# && mkdir curl_build && cd curl_build \
# && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN} -DCMAKE_BUILD_TYPE=Release -DBUILD_STATIC_LIBS=1 -DBUILD_SHARED_LIBS=0 -DBUILD_LIBCURL_DOCS=0 -DBUILD_MISC_DOCS=0 ../curl \
# && make -j${JOBS_COUNT} && make install && cd /soft && rm -rf curl curl_build
RUN unzip -o /soft/curl-8.11.0_2-win${WIN_BITS}-mingw.zip -d /soft/tmp \
 && cp -rfv /soft/tmp/curl*win${WIN_BITS}-mingw/bin/* ${INSTALL_DIR}/bin/ \
 && cp -rfv /soft/tmp/curl*win${WIN_BITS}-mingw/lib/* ${INSTALL_DIR}/lib/ \
 && cp -rfv /soft/tmp/curl*win${WIN_BITS}-mingw/include/* ${INSTALL_DIR}/include/ \
 && rm -rf /soft/tmp


#
# Build another libraries and tools
# compile and install
#

# 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 \
 && mkdir -p ${INSTALL_DIR}/build/assimp && cd ${INSTALL_DIR}/build/assimp \
 && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release \
 -DASSIMP_BUILD_ASSIMP_TOOLS=0 -DASSIMP_BUILD_TESTS=0 -DASSIMP_WARNINGS_AS_ERRORS=0 \
 -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN} /soft/assimp-${CUR_ASSIMP_VERSION} \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/assimp-${CUR_ASSIMP_VERSION}


# GTest
WORKDIR /soft
RUN git clone -b v1.17.x https://github.com/google/googletest.git \
 && mkdir googletest/build && cd googletest/build \
 && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN} .. \
 && make -j${JOBS_COUNT} && make install && cd /soft && rm -rf googletest


# 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 ${INSTALL_DIR}/build/sodium && cd ${INSTALL_DIR}/build/sodium \
 && sh -c 'CFLAGS="-O3 -fomit-frame-pointer ${SODIUM_M_ARG}" LDFLAGS="-O3 -fomit-frame-pointer ${SODIUM_M_ARG}" /soft/libsodium-${CUR_SODIUM_VERSION}/configure --host=${WIN_TRIPPLET} --prefix=${INSTALL_DIR}/ --exec-prefix=${INSTALL_DIR} && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf ${INSTALL_DIR}/build/sodium && rm -rf /soft/libsodium-*


# fftw3
WORKDIR /soft
RUN wget -v 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 \
 && cd /soft/fftw-* && patch < /soft/fftw.cmake.patch \
 && mkdir -p ${INSTALL_DIR}/build/fftw3 && cd ${INSTALL_DIR}/build/fftw3 \
 && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -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=${CMAKE_TOOLCHAIN} /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -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=${CMAKE_TOOLCHAIN} /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -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=${CMAKE_TOOLCHAIN} /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && rm -rf /soft/fftw-*


# markdown
RUN mkdir -p /soft/discount-${CUR_DISCOUNT_VERSION}/cmake && mkdir -p /soft/linux/build/discount && mkdir -p ${INSTALL_DIR}/build/discount
WORKDIR /soft
RUN wget -v 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
RUN patch < /soft/discount.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 ${INSTALL_DIR}/build/discount && mv /soft/linux/build/mktags /usr/sbin/
WORKDIR ${INSTALL_DIR}/build/discount
RUN cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN} -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-*/cmake \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/discount-*


# OpenSSL
#WORKDIR /soft
#RUN wget -v https://github.com/openssl/openssl/releases/download/openssl-${CUR_OPENSSL_VERSION}/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 ${INSTALL_DIR}/build/openssl && cd ${INSTALL_DIR}/build/openssl \
# && /soft/openssl-${CUR_OPENSSL_VERSION}/Configure --cross-compile-prefix=${WIN_TRIPPLET}- \
#	--prefix=${INSTALL_DIR}/ --release no-idea no-mdc2 no-rc5 no-tests shared mingw \
# && make -j${JOBS_COUNT} && make install \
# && cd /soft && rm -rf ${INSTALL_DIR}/build/openssl && rm -rf /soft/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 ${INSTALL_DIR}/build/openssl && cd ${INSTALL_DIR}/build/openssl \
 && /soft/openssl-OpenSSL_${CUR_OPENSSL_VERSION}/Configure --cross-compile-prefix=${WIN_TRIPPLET}- \
	--prefix=${INSTALL_DIR}/ --release no-idea no-mdc2 no-rc5 no-tests shared mingw \
 && make -j${JOBS_COUNT} && make install \
 && cd /soft && rm -rf ${INSTALL_DIR}/build/openssl && rm -rf /soft/openssl-OpenSSL_*


# Qt
ENV QT_SRC=/soft/qt-everywhere-src-${CUR_QT_VERSION}
WORKDIR /soft
RUN wget -v https://mirrors.ustc.edu.cn/qtproject/archive/qt/${CUR_QT_VERSION_MAJOR}/${CUR_QT_VERSION}/single/qt-everywhere-opensource-src-${CUR_QT_VERSION}.tar.xz \
 && tar -xf qt-everywhere-opensource-src-${CUR_QT_VERSION}.tar.xz -C /soft \
 && rm -f qt-everywhere-opensource-src-${CUR_QT_VERSION}.tar.xz \
 && cd ${QT_SRC}/qtdeclarative/src/plugins/scenegraph/d3d12 \
 && patch < /soft/qt-5.15.18-qml-sg-d3d12-cross.patch \
 && cd ${QT_SRC}/qtconnectivity/src/bluetooth \
 && patch < /soft/qt-5.15.18-bluetooth-mingw.patch \
 && cd ${QT_SRC}/qtlocation/src/3rdparty/mapbox-gl-native/include/mbgl/util \
 && patch < /soft/qt-5.15.18-location-mbgl-util-include.patch \
 && cd ${QT_SRC}/qtlocation/src/3rdparty/mapbox-gl-native/deps/rapidjson/1.1.0/include/rapidjson \
 && patch < /soft/qt-5.15.18-location-mbgl-rapidjson.patch \
 && cd ${QT_SRC}/qtlocation/src/3rdparty/mapbox-gl-native/deps/variant/1.1.4/include/mapbox \
 && patch < /soft/qt-5.15.18-location-mbgl-mapbox-include.patch \
 && cd ${QT_SRC}/qttools/src/assistant/qcollectiongenerator \
 && patch < /soft/qt-5.15.18-qcollectiongenerator.patch \
 && mkdir -p ${INSTALL_DIR}/build/qt \
 && cd ${INSTALL_DIR}/build/qt \
 && ${QT_SRC}/configure -opensource -confirm-license -release -shared -no-pch -silent \
			-xplatform win32-g++ -extprefix "${INSTALL_DIR}" -c++std c++17 \
			-sysroot ${SYSROOT} -gcc-sysroot -device-option CROSS_COMPILE=/usr/bin/${WIN_TRIPPLET}- \
			-opengl desktop -qt-libpng -system-zlib -qt-pcre -no-xcb \
			-make tools -nomake tests -nomake examples -no-compile-examples \
			-skip qtwebengine -skip qtwebglplugin -skip doc -skip qt3d -skip qtactiveqt -skip qtquick3d \
			-openssl-linked OPENSSL_PREFIX="${INSTALL_DIR}/" \
			PSQL_INCDIR="${SYSROOT}/include/postgresql" PSQL_LIBDIR="${SYSROOT}/lib" \
 && make -j${JOBS_QT_COUNT} && make install -j${JOBS_QT_COUNT} && rm -rf ./* && rm -rf /soft/qt-everywhere-src-*


# OpenCV
WORKDIR /soft
RUN wget -v https://github.com/opencv/opencv/archive/refs/tags/${CUR_OPENCV_VERSION}.tar.gz \
 && tar -xf ${CUR_OPENCV_VERSION}.tar.gz -C /soft \
 && rm -f ${CUR_OPENCV_VERSION}.tar.gz \
 && mkdir -p ${INSTALL_DIR}/build/opencv && cd ${INSTALL_DIR}/build/opencv \
 && cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_LIBJPEG_TURBO_SIMD=0 -DBUILD_PERF_TESTS=0 -DBUILD_TESTS=0 -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN} /soft/opencv-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/opencv-*


# HDF5
ENV CUR_HDF5_VERSION=1_14_3
WORKDIR /soft
RUN git clone -b hdf5_${CUR_HDF5_VERSION} --depth 1 https://github.com/HDFGroup/hdf5.git \
 && mkdir hdf_build && cd hdf_build \
 && cmake -C/soft/hdf5_cache.cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN} -DCMAKE_BUILD_TYPE=Release -DHDF5_BUILD_CPP_LIB=1 -DBUILD_TESTING=0 ../hdf5 \
 && make -j${JOBS_COUNT} && make install && cd /soft && rm -rf hdf5 hdf5_build /soft/hdf5_cache.cmake


RUN chmod +r ${INSTALL_DIR}/bin/*

ENV BUILD_TYPE=Release

WORKDIR /soft
