ARG DOCKER_PREFIX=wapmobil/
FROM ${DOCKER_PREFIX}osxcross
ARG JOBS_COUNT=4
ARG JOBS_QT_COUNT=2

COPY toolchain-Darwin.cmake /soft/

ENV CUR_SODIUM_VERSION=1.0.18
ENV CUR_FFTW_VERSION=3.3.8
ENV CUR_ASSIMP_VERSION=4.1.0
ENV CUR_DISCOUNT_VERSION=2.2.6
ENV CUR_MICROHTTPD_VERSION=0.9.72
ENV CUR_QT_VERSION_MAJOR=5.15
ENV CUR_QT_VERSION=5.15.2
ENV QT_SELECT=5
ENV _DARWIN=apple-darwin20.4




ENV ARCH=x86_64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# 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/osx/build/sodium && cd /soft/osx/build/sodium \
 && sh -c 'CFLAGS="-arch ${ARCH} -mmacosx-version-min=10.8 -march=core2 -O3" LDFLAGS="-arch ${ARCH} -mmacosx-version-min=10.8 -march=core2" LD=${CROSS_COMPILE}ld CC=${CROSS_COMPILE}clang CXX=${CROSS_COMPILE}clang++ /soft/libsodium-${CUR_SODIUM_VERSION}/configure --host=${ARCH}-${_DARWIN} --prefix=/soft/osx/${ARCH}/ && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/osx/build/sodium && rm -rf /soft/libsodium-* \
 && /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libsodium.23.dylib /soft/osx/${ARCH}/lib/libsodium.23.dylib


ENV ARCH=aarch64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# 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/osx/build/sodium && cd /soft/osx/build/sodium \
 && sh -c 'CFLAGS="-arch arm64 -mmacosx-version-min=10.8 -O3" LDFLAGS="-arch arm64 -mmacosx-version-min=10.8" LD=${CROSS_COMPILE}ld CC=${CROSS_COMPILE}clang CXX=${CROSS_COMPILE}clang++ /soft/libsodium-${CUR_SODIUM_VERSION}/configure --host=${ARCH}-${_DARWIN} --prefix=/soft/osx/${ARCH}/ && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/osx/build/sodium && rm -rf /soft/libsodium-* \
 && /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libsodium.23.dylib /soft/osx/${ARCH}/lib/libsodium.23.dylib




ENV ARCH=x86_64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# 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/osx/build/fftw3 && cd /soft/osx/build/fftw3 \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && rm -rf /soft/fftw-*
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libfftw3.dylib   /soft/osx/${ARCH}/lib/libfftw3.dylib
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libfftw3f.dylib  /soft/osx/${ARCH}/lib/libfftw3f.dylib
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libfftw3l.dylib  /soft/osx/${ARCH}/lib/libfftw3l.dylib


ENV ARCH=aarch64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# 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/osx/build/fftw3 && cd /soft/osx/build/fftw3 \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/fftw-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* \
 && rm -rf /soft/fftw-*
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libfftw3.dylib   /soft/osx/${ARCH}/lib/libfftw3.dylib
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libfftw3f.dylib  /soft/osx/${ARCH}/lib/libfftw3f.dylib
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libfftw3l.dylib  /soft/osx/${ARCH}/lib/libfftw3l.dylib




ENV ARCH=x86_64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# 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/osx/build/assimp && cd /soft/osx/build/assimp \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/assimp-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/assimp-*
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libassimp.4.dylib  /soft/osx/${ARCH}/lib/libassimp.4.dylib


ENV ARCH=aarch64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# 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/osx/build/assimp && cd /soft/osx/build/assimp \
 && cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -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-Darwin.cmake /soft/assimp-* \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/assimp-*
RUN /soft/osxcross/target/bin/${ARCH}-${_DARWIN}-install_name_tool -id @rpath/libassimp.4.dylib  /soft/osx/${ARCH}/lib/libassimp.4.dylib




# 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/osx/build/discount && mv /soft/linux/build/mktags /soft/osx/build/discount/mktags


ENV ARCH=x86_64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

WORKDIR /soft/osx/build/discount
RUN cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Darwin.cmake -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-*/cmake \
 && make install -j${JOBS_COUNT} && rm -rf ./*


ENV ARCH=aarch64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# markdown
RUN cmake -DCMAKE_SYSTEM_PROCESSOR=${ARCH} -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/soft/osx/${ARCH} -DCMAKE_TOOLCHAIN_FILE=/soft/toolchain-Darwin.cmake -DDISCOUNT_ONLY_LIBRARY=1 /soft/discount-*/cmake \
 && make install -j${JOBS_COUNT} && rm -rf ./* && rm -rf /soft/discount-*




ENV ARCH=x86_64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# microhttpd
WORKDIR /soft/
RUN wget -nv https://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz \
 && tar -xf /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz -C /soft/ \
 && rm /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz \
 && mkdir -p /soft/osx/build/microhttpd && cd /soft/osx/build/microhttpd \
 && sh -c 'CFLAGS="-arch ${ARCH} -mmacosx-version-min=10.8 -march=core2 -O3" LDFLAGS="-arch ${ARCH} -mmacosx-version-min=10.8 -march=core2" LD=${CROSS_COMPILE}ld CC=${CROSS_COMPILE}clang CXX=${CROSS_COMPILE}clang++ /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}/configure --host=${ARCH}-${_DARWIN} --prefix=/soft/osx/${ARCH}/ --enable-shared=no --enable-static=yes && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/osx/build/microhttpd && rm -rf /soft/libmicrohttpd-*


ENV ARCH=aarch64
ENV CROSS_COMPILE=${ARCH}-${_DARWIN}-

# microhttpd
WORKDIR /soft/
RUN wget -nv https://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz \
 && tar -xf /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz -C /soft/ \
 && rm /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}.tar.gz \
 && mkdir -p /soft/osx/build/microhttpd && cd /soft/osx/build/microhttpd \
 && sh -c 'CFLAGS="-arch arm64 -mmacosx-version-min=10.8 -O3" LDFLAGS="-arch arm64 -mmacosx-version-min=10.8" LD=${CROSS_COMPILE}ld CC=${CROSS_COMPILE}clang CXX=${CROSS_COMPILE}clang++ /soft/libmicrohttpd-${CUR_MICROHTTPD_VERSION}/configure --host=${ARCH}-${_DARWIN} --prefix=/soft/osx/${ARCH}/ --enable-shared=no --enable-static=yes && make install -j${JOBS_COUNT}' \
 && cd /soft && rm -rf /soft/osx/build/microhttpd && rm -rf /soft/libmicrohttpd-*




ENV ARCH=x86_64

# Qt
ENV SYSROOT=/soft/osxcross/target/SDK/MacOSX11.3.sdk
ENV CROSS_COMPILE=/soft/osxcross/target/bin/${ARCH}-${_DARWIN}-
WORKDIR /soft
RUN wget -nv http://download.qt.io/official_releases/qt/${CUR_QT_VERSION_MAJOR}/${CUR_QT_VERSION}/single/qt-everywhere-src-${CUR_QT_VERSION}.tar.xz \
 && tar -xf qt-everywhere-src-${CUR_QT_VERSION}.tar.xz -C /soft \
 && rm -f qt-everywhere-src-${CUR_QT_VERSION}.tar.xz \
 && mkdir -p /soft/osx/build/qt && cd /soft/osx/build/qt \
 && sh -c 'echo "#!/bin/sh" > /usr/bin/xcode-select' \
 && sh -c 'echo "echo ${SYSROOT}" >> /usr/bin/xcode-select' \
 && chmod +x /usr/bin/xcode-select \
 && sh -c 'echo "#!/bin/sh" > /usr/bin/xcrun' \
 && sh -c 'echo "/soft/osxcross/target/bin/xcrun \"\$@\" 2>/dev/null" >> /usr/bin/xcrun' \
 && chmod +x /usr/bin/xcrun \
 && sed -i 's/license/version/' /soft/qt-everywhere-src-${CUR_QT_VERSION}/qtbase/mkspecs/features/mac/default_pre.prf \
 && /soft/qt-everywhere-src-${CUR_QT_VERSION}/configure -opensource -confirm-license -release -shared -no-pch -xplatform macx-clang -extprefix "/soft/osx/${ARCH}" \
			-sysroot ${SYSROOT} -gcc-sysroot -sdk macosx -device-option CROSS_COMPILE=${CROSS_COMPILE} \
            -qt-libpng -system-zlib -qt-pcre \
            -make tools -nomake tests -nomake examples -no-compile-examples \
            -skip qtwebengine -skip qtwebglplugin -skip doc -skip qt3d \
 && make -j${JOBS_QT_COUNT} && make install && rm -rf ./* && rm -rf /soft/qt-everywhere-src-*


WORKDIR /soft
 
C:/sdk/MinGW/x64/bin;C:/sdk/Qt_5.15.0_x64/bin;C:/sdk/CMake/bin;C:/sdk/llvm-10.0.0_x64/bin;C:/Windows/system32;C:/Perl64/site/bin;C:/Perl64/bin;C:/Users/peri4/AppData/Local/Programs/Python/Python37-32/Scripts/;C:/Users/peri4/AppData/Local/Programs/Python/Python37-32/;c:/Ruby25/bin/