# Reproducible build environment for the Windows x64 FFmpeg used by PlayaMedia.
#
# Cross-compiled from Linux with mingw-w64, the same way the BtbN autobuilds we
# used to download are made — but with our own configure posture, so the result
# is LGPL v2.1 with one external library instead of LGPL v3 with about forty-five.
# See third_party/ffmpeg/NOTICE.md.
#
# Build:  docker build -t playamedia-ffmpeg-windows .
# Run:    docker run --rm -v <repo>\Plugins\PlayaMedia\third_party\ffmpeg:/out \
#             playamedia-ffmpeg-windows
#
# The image is ~1.5 GB. Keep it: rebuilding FFmpeg for a new ref then takes
# minutes instead of re-fetching the toolchain.

FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        ca-certificates \
        curl \
        file \
        git \
        mingw-w64 \
        nasm \
        pkg-config \
    && rm -rf /var/lib/apt/lists/*

# Debian defaults the cross-gcc to its posix-threads variant, which drags
# libwinpthread-1.dll into anything it links. FFmpeg uses --enable-w32threads
# and needs none of it; picking the win32 variant makes an accidental
# dependency impossible rather than merely unlikely.
RUN update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-win32 \
    && update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-win32

# zlib, static, built here rather than taken from Debian: the Debian package
# ships an import library too, and FFmpeg would then link against zlib1.dll,
# which we would have to ship and keep in the notices. Static means the only
# thing we distribute is the FFmpeg DLLs themselves.
#
# It is here at all because the Android build gets zlib from the platform
# (libz.so is an Android system library) and identical format coverage across
# platforms is the whole point — see build_ffmpeg_windows.sh. It is also the
# ONLY external library in this build; its version and license text are
# recorded in the prefix.
#
# Cloned by exact tag, like FFmpeg in the Android image: a tag can be named in
# the corresponding-source notice, a tarball URL cannot always be.
ARG ZLIB_REF=v1.3.1
RUN git clone --branch "${ZLIB_REF}" --depth 1 -q \
        https://github.com/madler/zlib.git /tmp/zlib \
    && make -C /tmp/zlib -f win32/Makefile.gcc libz.a PREFIX=x86_64-w64-mingw32- \
    && mkdir -p /opt/zlib-mingw/include /opt/zlib-mingw/lib \
    && cp /tmp/zlib/zlib.h /tmp/zlib/zconf.h /opt/zlib-mingw/include/ \
    && cp /tmp/zlib/libz.a /opt/zlib-mingw/lib/ \
    && cp /tmp/zlib/LICENSE /opt/zlib-mingw/zlib-LICENSE \
    && printf '%s\n' "${ZLIB_REF}" > /opt/zlib-mingw/VERSION \
    && git -C /tmp/zlib rev-parse HEAD > /opt/zlib-mingw/COMMIT \
    && rm -rf /tmp/zlib
ENV ZLIB_PREFIX="/opt/zlib-mingw"

COPY build_ffmpeg_windows.sh /usr/local/bin/build_ffmpeg_windows.sh
RUN chmod +x /usr/local/bin/build_ffmpeg_windows.sh

VOLUME /out
ENTRYPOINT ["/usr/local/bin/build_ffmpeg_windows.sh"]
