# vim: ft=dockerfile:
# Dockerfile to build nghttp2 android binary
#
# $ sudo docker build -t nghttp2-android - < Dockerfile.android
#
# After successful build, android binaries are located under
# /root/build/nghttp2.  You can copy the binary using docker cp.  For
# example, to copy nghttpx binary to host file system location
# /path/to/dest, do this:
#
# $ sudo docker run -v /path/to/dest:/out nghttp2-android cp /root/build/nghttp2/src/nghttpx /out


# Only use standalone-toolchain for reduce size
FROM ubuntu:22.04
MAINTAINER Tatsuhiro Tsujikawa

ENV NDK_VERSION r26d
ENV NDK /root/android-ndk-$NDK_VERSION
ENV TOOLCHAIN $NDK/toolchains/llvm/prebuilt/linux-x86_64
ENV TARGET aarch64-linux-android
ENV API 33
ENV AR $TOOLCHAIN/bin/llvm-ar
ENV CC $TOOLCHAIN/bin/$TARGET$API-clang
ENV CXX $TOOLCHAIN/bin/$TARGET$API-clang++
ENV LD $TOOLCHAIN/bin/ld
ENV RANDLIB $TOOLCHAIN/bin/llvm-ranlib
ENV STRIP $TOOLCHAIN/bin/llvm-strip
ENV PREFIX /root/usr/local

WORKDIR /root
RUN apt-get update && \
    apt-get install -y unzip make binutils autoconf \
      automake autotools-dev libtool pkg-config git \
      curl dpkg-dev libxml2-dev genisoimage libc6-i386 \
      lib32stdc++6 && \
    rm -rf /var/cache/apt/*

# Download NDK
RUN curl -L -O https://dl.google.com/android/repository/android-ndk-$NDK_VERSION-linux.zip && \
   unzip -q android-ndk-$NDK_VERSION-linux.zip && \
   rm android-ndk-$NDK_VERSION-linux.zip

# Setup version of libraries
ENV OPENSSL_VERSION 1.1.1w
ENV LIBEV_VERSION 4.33
ENV ZLIB_VERSION 1.3.1
ENV CARES_VERSION 1.18.1
ENV NGHTTP2_VERSION master

WORKDIR /root/build
RUN curl -L -O https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz && \
    tar xf openssl-$OPENSSL_VERSION.tar.gz && \
    rm openssl-$OPENSSL_VERSION.tar.gz

WORKDIR /root/build/openssl-$OPENSSL_VERSION
RUN export ANDROID_NDK_HOME=$NDK PATH=$TOOLCHAIN/bin:$PATH && \
    ./Configure no-shared --prefix=$PREFIX android-arm64 && \
    make && make install_sw

WORKDIR /root/build
RUN curl -L -O http://dist.schmorp.de/libev/Attic/libev-$LIBEV_VERSION.tar.gz && \
    tar xf libev-$LIBEV_VERSION.tar.gz && \
    rm libev-$LIBEV_VERSION.tar.gz

WORKDIR /root/build/libev-$LIBEV_VERSION
RUN ./configure \
    --disable-dependency-tracking \
    --host=$TARGET \
    --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \
    --prefix=$PREFIX \
    --disable-shared \
    --enable-static \
    CPPFLAGS=-I$PREFIX/include \
    LDFLAGS=-L$PREFIX/lib && \
    make install

WORKDIR /root/build
RUN curl -L -O https://github.com/madler/zlib/releases/download/v$ZLIB_VERSION/zlib-$ZLIB_VERSION.tar.gz && \
    tar xf zlib-$ZLIB_VERSION.tar.gz && \
    rm zlib-$ZLIB_VERSION.tar.gz

WORKDIR /root/build/zlib-$ZLIB_VERSION
RUN HOST=$TARGET \
    ./configure \
    --prefix=$PREFIX \
    --libdir=$PREFIX/lib \
    --includedir=$PREFIX/include \
    --static && \
    make install


WORKDIR /root/build
RUN curl -L -O https://github.com/c-ares/c-ares/releases/download/cares-1_18_1/c-ares-$CARES_VERSION.tar.gz && \
    tar xf c-ares-$CARES_VERSION.tar.gz && \
    rm c-ares-$CARES_VERSION.tar.gz

WORKDIR /root/build/c-ares-$CARES_VERSION
RUN ./configure \
      --disable-dependency-tracking \
      --host=$TARGET \
      --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \
      --prefix=$PREFIX \
      --disable-shared && \
    make install

WORKDIR /root/build
RUN git clone https://github.com/nghttp2/nghttp2 -b $NGHTTP2_VERSION --depth 1
WORKDIR /root/build/nghttp2
RUN autoreconf -i && \
    ./configure \
    --disable-dependency-tracking \
    --enable-app \
    --disable-shared \
    --host=$TARGET \
    --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE` \
    --without-libxml2 \
    --disable-examples \
    --disable-threads \
      CPPFLAGS="-fPIE -I$PREFIX/include" \
      PKG_CONFIG_LIBDIR="$PREFIX/lib/pkgconfig" \
      LDFLAGS="-fPIE -pie -L$PREFIX/lib" && \
    make && \
    $STRIP src/nghttpx src/nghttpd src/nghttp
