ARG DOCKER_PREFIX=wapmobil/
FROM ${DOCKER_PREFIX}debian-base

ARG JOBS_COUNT=4
ARG QT_DOWNLOAD_SERVER=shstk.ru/files

ENV CUR_SODIUM_VERSION=1.0.18
ENV CUR_FFTW_VERSION=3.3.8
ENV CUR_QT_VERSION_MAJOR=5.14
ENV CUR_QT_VERSION=5.14.1


# dev soft for our SDK
RUN mkdir -p /usr/share/man/man1 && apt-get update \
 && apt-get install -y cpio libglu1-mesa-dev libgl1-mesa-dev libwayland-dev libncurses-dev flex bison gperf \
 python python-pip python-setuptools python-serial python-click python-cryptography python-future python-pyparsing python-pyelftools \
 ccache libffi-dev libssl-dev doxygen graphviz libx11-dev libxkbcommon-dev libpkgconfig-perl libfreetype6-dev libfontconfig1-dev \
 lzma-dev liblzma-dev libxml2-dev libbz2-dev libmpc-dev libmpfr-dev libgmp-dev libicu-dev libusb-1.0-0-dev libreadline-dev \
 build-essential android-sdk openjdk-11-jdk && rm -rf /var/cache/apt/archives/*


# Android SDK and NDK
WORKDIR /soft/android

ENV ANDROID_HOME=/usr/lib/android-sdk
ENV ANDROID_NDK_HOME=${ANDROID_HOME}/ndk-bundle
ENV ANDROID_TOOLCHAIN=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake
ENV NDK_PLATFORM="android-21"

RUN wget -nv -O commandlinetools.zip https://dl.google.com/android/repository/commandlinetools-linux-6200805_latest.zip?hl=ru \
&& unzip -o commandlinetools.zip -d ${ANDROID_HOME} && rm -f commandlinetools.zip
RUN  yes | ${ANDROID_HOME}/tools/bin/sdkmanager --sdk_root=${ANDROID_HOME} --licenses
RUN ${ANDROID_HOME}/tools/bin/sdkmanager --sdk_root=${ANDROID_HOME} --update
RUN ${ANDROID_HOME}/tools/bin/sdkmanager --sdk_root=${ANDROID_HOME} "platforms;android-21" "platforms;android-29" "platforms;android-30" "platforms;android-31" "platforms;android-32" "platforms;android-33" "platforms;android-34" "platforms;android-35" "ndk-bundle" "build-tools;28.0.3" "platform-tools" "tools"

COPY android.toolchain.cmake.add /soft/android/
RUN cat /soft/android/android.toolchain.cmake.add >> ${ANDROID_TOOLCHAIN}
RUN mkdir -p /soft/android/armeabi-v7a && mkdir -p /soft/android/arm64-v8a && mkdir -p /soft/android/x86 && mkdir -p /soft/android/x86_64


# 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
WORKDIR /soft/libsodium-${CUR_SODIUM_VERSION}

RUN TARGET_ARCH=armv7-a \
	CFLAGS="-O3 -mfloat-abi=softfp -mfpu=vfpv3-d16 -mthumb -marm -march=armv7-a" \
	ARCH=arm \
	HOST_COMPILER=arm-linux-androideabi \
	dist-build/android-build.sh && \
	cp -rfv /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-armv7-a/* /soft/android/armeabi-v7a/ && \
	rm -rf /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-* && \
	make distclean && rm -rf ./android-toolchain*
RUN TARGET_ARCH=armv8-a \
	CFLAGS="-O3 -march=armv8-a" \
	ARCH=arm64 \
	HOST_COMPILER=aarch64-linux-android \
	dist-build/android-build.sh && \
	cp -rfv /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-armv8-a/* /soft/android/arm64-v8a/ && \
	rm -rf /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-* && \
	make distclean && rm -rf ./android-toolchain*
RUN TARGET_ARCH=i686 \
	CFLAGS="-O3 -march=i686" \
	ARCH=x86 \
	HOST_COMPILER=i686-linux-android \
	dist-build/android-build.sh && \
	cp -rfv /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-i686/* /soft/android/x86/ && \
	rm -rf /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-* && \
	make distclean && rm -rf ./android-toolchain*
RUN TARGET_ARCH=westmere \
	CFLAGS="-O3 -march=westmere" \
	ARCH=x86_64 \
	HOST_COMPILER=x86_64-linux-android \
	dist-build/android-build.sh && \
	cp -rfv /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-westmere/* /soft/android/x86_64/ && \
	rm -rf /soft/libsodium-${CUR_SODIUM_VERSION}/libsodium-android-* && \
	make distclean && rm -rf ./android-toolchain*
WORKDIR /soft
RUN 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
WORKDIR /soft/fftw-${CUR_FFTW_VERSION}_build

ENV _CUR_ABI=armeabi-v7a
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=1 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=1 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*

ENV _CUR_ABI=arm64-v8a
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=1 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=1 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*

ENV _CUR_ABI=x86
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=1 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=1 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*

ENV _CUR_ABI=x86_64
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=1 -DENABLE_LONG_DOUBLE=0 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*
RUN cmake -DCMAKE_INSTALL_PREFIX=/soft/android/${_CUR_ABI} -DENABLE_FLOAT=0 -DENABLE_LONG_DOUBLE=1 -DENABLE_QUAD_PRECISION=0 -DENABLE_THREADS=1 -DWITH_COMBINED_THREADS=1 -DCMAKE_TOOLCHAIN_FILE=${ANDROID_TOOLCHAIN} -DANDROID_PLATFORM=${NDK_PLATFORM} -DANDROID_ABI=${_CUR_ABI} /soft/fftw-${CUR_FFTW_VERSION} && make install -j${JOBS_COUNT} && rm -rf ./*


# Qt
WORKDIR /soft/android

RUN wget -nv http://${QT_DOWNLOAD_SERVER}/qt-android-${CUR_QT_VERSION}.zip \
 && unzip -o qt-android-${CUR_QT_VERSION}.zip -d /soft/android/qt \
 && rm -f qt-android-${CUR_QT_VERSION}.zip
WORKDIR /soft/android/qt/lib/cmake/Qt5
COPY Qt5Config.patch /soft/android/qt/lib/cmake/Qt5
RUN patch < Qt5Config.patch

ENV BUILD_TYPE=Release

WORKDIR /soft
