name: FFmpeg native build
on:
  workflow_dispatch:
    inputs:
      rev:
        description: 'FFmpeg/FFmpeg commit to build'
        required: true

env:
  DECODERS: "h264,vp8"
  MACOSX_DEPLOYMENT_TARGET: "11.0"

jobs:
  build:
    name: ${{ matrix.platform.name }}
    runs-on: ${{ matrix.platform.os }}
    strategy:
      fail-fast: false
      matrix:
        platform:
          - { name: win-x64, os: ubuntu-20.04, flags: "--arch=x86_64 --target-os=mingw32 --cross-prefix=x86_64-w64-mingw32- --disable-w32threads --extra-ldflags=\"-static-libgcc -static-libstdc++ -static\"" }
          - { name: linux-x64, os: ubuntu-20.04, flags: "--arch=x86_64 --extra-ldflags=\"-static-libgcc -static-libstdc++\"" }
          - { name: osx-x64, os: macos-latest, flags: "--arch=x86_64 --install-name-dir=\"@rpath\" --enable-cross-compile --cc=\"clang -arch x86_64\" --disable-xlib" }
          - { name: osx-arm64, os: macos-latest, flags: "--arch=arm64 --install-name-dir=\"@rpath\" --enable-neon --enable-cross-compile --cc=\"clang -arch arm64\" --disable-xlib" }
    steps:
      - name: Setup Linux common dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get install yasm                 \
                               make                 \
                               automake             \
                               autoconf             \
                               git                  \
                               pkg-config           \
                               libtool-bin          \
                               nasm
      - name: Setup MinGW64 dependencies
        if: ${{ matrix.platform.name == 'win-x64' }}
        run: |
          sudo apt-get install gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64
      - name: Setup nasm for osx-x64 dependencies
        if: ${{ matrix.platform.name == 'osx-x64' }}
        run: |
          brew install nasm

      - uses: actions/checkout@v2
        with:
          repository: FFmpeg/FFmpeg
          ref: ${{ github.event.inputs.rev }}
      - name: Configure FFmpeg build
        run: |
          ./configure --disable-everything                  \
                      --disable-static                      \
                      --disable-doc                         \
                      --disable-programs                    \
                      --disable-swscale                     \
                      --disable-avformat                    \
                      --disable-swresample                  \
                      --disable-avdevice                    \
                      --disable-avfilter                    \
                      --disable-debug                       \
                      --enable-avcodec                      \
                      --enable-shared                       \
                      --enable-decoder=${{ env.DECODERS }}  \
                      --enable-lto                          \
                      --enable-stripping                    \
                      ${{ matrix.platform.flags }}          \
                      --prefix=release_output
      - name: "Build FFmpeg"
        run: |
          make -j4 && make install
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: ${{ matrix.platform.name }}-binaries
          path: |
            release_output/*
