# Cross-compilation environment for a from-source, LGPL-only, win64 FFmpeg
# build with libsrt + hardware encoders + AV1/Vorbis. See README.md.
#
# 24.04 (not 22.04) is required: its mingw-w64 package is new enough to carry
# the Media Foundation headers/import-libs (mfplat, mfuuid, mfreadwrite,
# strmiids, codecapi.h) that --enable-mediafoundation needs.
FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        gcc-mingw-w64-x86-64 \
        g++-mingw-w64-x86-64 \
        mingw-w64-tools \
        cmake \
        meson \
        ninja-build \
        pkg-config \
        nasm \
        yasm \
        autoconf \
        automake \
        libtool \
        git \
        python3 \
        curl \
        ca-certificates \
        unzip \
        rsync \
    && rm -rf /var/lib/apt/lists/*

# Use the posix-threading mingw variant (required for FFmpeg's pthread-based
# threading model) rather than the win32-threading alternative Ubuntu also
# ships.
RUN update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix \
    && update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix

# Two separate sysroots, one per linkage variant. FFmpeg's own configure
# doesn't reliably force static-vs-shared linkage of *dependency* libraries
# when both a .a and a .dll/.dll.a for the same lib are visible on the same
# path -- the linker just picks one. Keeping the static-only and shared-only
# builds of every dependency in physically separate prefixes removes the
# ambiguity: point PKG_CONFIG_PATH/-L at CROSS_PREFIX_STATIC for the static
# FFmpeg pass, at CROSS_PREFIX_SHARED for the shared pass.
ENV TARGET_TRIPLE=x86_64-w64-mingw32
ENV CROSS_PREFIX_STATIC=/opt/mingw-sysroot-static
ENV CROSS_PREFIX_SHARED=/opt/mingw-sysroot-shared
ENV BUILD_ROOT=/build

RUN mkdir -p ${CROSS_PREFIX_STATIC} ${CROSS_PREFIX_SHARED} ${BUILD_ROOT} /out

WORKDIR /build
COPY scripts/ /build/scripts/
COPY licenses/ /build/licenses/
COPY versions.env /build/versions.env
# Also copy this Dockerfile itself so package.sh can bundle the whole
# build environment (Dockerfile + scripts/ + licenses/ + versions.env)
# into out/build/. This lets the plugin ship the exact, reproducible
# recipe for its bundled FFmpeg binary alongside it, satisfying LGPL's
# "accompany with corresponding source" for every statically-linked
# dependency (see build-libmp3lame.sh in particular, which patches LAME).
# README.md/PACKAGING.md are deliberately NOT copied here -- they're
# internal-only docs (see their own headers), not meant to accompany a
# distributed binary.
COPY Dockerfile /build/Dockerfile
RUN chmod +x /build/scripts/*.sh

ENTRYPOINT ["/build/scripts/entrypoint.sh"]
