FROM debian:13 as build

ARG NGHTTP2_BRANCH=master

RUN <<EOF
set -e

# Enable backports to get libelf-dev that fixes static linking issue.
#
# - https://bugs-devel.debian.org/cgi-bin/bugreport.cgi?bug=1086675
# - https://sourceware.org/bugzilla/show_bug.cgi?id=32293
cat > /etc/apt/sources.list.d/debian-backports.sources <<'EOT'
Types: deb deb-src
URIs: http://deb.debian.org/debian
Suites: trixie-backports
Components: main
Enabled: yes
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
EOT

apt-get update
apt-get install -y --no-install-recommends \
    git clang-19 make binutils autoconf automake autotools-dev libtool \
    pkg-config cmake cmake-data \
    zlib1g-dev libev-dev libjemalloc-dev ruby-dev libc-ares-dev bison \
    libbrotli-dev
apt-get install -y --no-install-recommends -t trixie-backports libelf-dev

git clone --recursive --shallow-submodules --depth 1 -b v5.4.0 \
    https://github.com/aws/aws-lc
cd aws-lc
export CC=clang-19 CXX=clang++-19
cmake -B build -DDISABLE_GO=ON
make -j$(nproc) -C build
cmake --install build
cd ..
rm -rf aws-lc

git clone --recursive --shallow-submodules --depth 1 -b v1.18.0 \
    https://github.com/ngtcp2/nghttp3
cd nghttp3
autoreconf -i
./configure --disable-dependency-tracking --enable-lib-only \
    CC=clang-19 CXX=clang++-19
make -j$(nproc)
make install-strip
cd ..
rm -rf nghttp3

git clone --recursive --shallow-submodules --depth 1 -b v1.25.0 \
    https://github.com/ngtcp2/ngtcp2
cd ngtcp2
autoreconf -i
./configure --disable-dependency-tracking --enable-lib-only \
    --with-boringssl \
    CC=clang-19 CXX=clang++-19 \
    LIBTOOL_LDFLAGS="-static-libtool-libs" \
    BORINGSSL_LIBS="-l:libssl.a -l:libcrypto.a" \
    PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig"
make -j$(nproc)
make install-strip
cd ..
rm -rf ngtcp2

git clone --depth 1 -b v1.7.0 https://github.com/libbpf/libbpf
cd libbpf
CC=clang-19 PREFIX=/usr/local make -C src install
cd ..
rm -rf libbpf

git clone --recursive --shallow-submodules --depth 1 -b $NGHTTP2_BRANCH \
    https://github.com/nghttp2/nghttp2
cd nghttp2
autoreconf -i
./configure --disable-dependency-tracking --disable-examples \
    --disable-hpack-tools \
    --with-mruby \
    --enable-http3 --with-libbpf \
    --with-libbrotlienc --with-libbrotlidec \
    CC=clang-19 CXX=clang++-19 \
    LIBTOOL_LDFLAGS="-static-libtool-libs" \
    OPENSSL_LIBS="-l:libssl.a -l:libcrypto.a" \
    LIBEV_LIBS="-l:libev.a" \
    JEMALLOC_LIBS="-l:libjemalloc.a" \
    LIBCARES_LIBS="-l:libcares.a" \
    ZLIB_LIBS="-l:libz.a" \
    LIBBPF_LIBS="-L/usr/local/lib64 -l:libbpf.a -l:libelf.a -l:libzstd.a" \
    LIBBROTLIENC_LIBS="-l:libbrotlienc.a -l:libbrotlicommon.a" \
    LIBBROTLIDEC_LIBS="-l:libbrotlidec.a -l:libbrotlicommon.a" \
    LDFLAGS="-static-libgcc -static-libstdc++" \
    PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig"
make -j$(nproc) install-strip
cd ..
rm -rf nghttp2
EOF

FROM gcr.io/distroless/base-nossl-debian13

COPY --from=build --link \
    /usr/local/bin/h2load \
    /usr/local/bin/nghttpx \
    /usr/local/bin/nghttp \
    /usr/local/bin/nghttpd \
    /usr/local/bin/
COPY --from=build --link /usr/local/lib/nghttp2/reuseport_kern.o \
    /usr/local/lib/nghttp2/
