#
# !reference [] Anchors
#

.source_code_rules:
  changes:
    - "**/*.asm"
    - "**/*.c"
    - "**/*.cc"
    - "**/*.cpp"
    - "**/CMakeLists.txt"
    - "**/*.cmake"
    - "**/*.h"
    - "**/*.in"
    - "**/.gitlab-ci.yml"
    - "**/*.patch"

.compiler-variables: &compiler-variables
  CFLAGS: -Werror -Wshadow -pipe
  CXXFLAGS: -Werror -Wshadow -pipe
  LDFLAGS: -Werror -Wshadow -pipe
  CMAKE_BUILD_TYPE: Release
  BUILD_SHARED_LIBS: "OFF"
  PREFIX_DIR: /usr/local
  FFMPEG_CONFIG_FLAGS: ""

.compiler-script:
  - export CFLAGS="$CFLAGS${EXTRA_CFLAGS:+ $EXTRA_CFLAGS}"
  - export CXXFLAGS="$CXXFLAGS${EXTRA_CXXFLAGS:+ $EXTRA_CXXFLAGS}"
  - export LDFLAGS="$LDFLAGS${EXTRA_LDFLAGS:+ $EXTRA_LDFLAGS}"

  - |
    cat << EOF
    Configuration
    ============
    CFLAGS="$CFLAGS"
    CXXFLAGS="$CXXFLAGS"
    LDFLAGS="$LDFLAGS"
    CMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE"
    BUILD_SHARED_LIBS="$BUILD_SHARED_LIBS"
    PREFIX_DIR="$PREFIX_DIR"
    ============
    Running
    ============
    cmake -B Build \\
      -DREPRODUCIBLE_BUILDS=ON \\
      -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE" \\
      -DBUILD_SHARED_LIBS="$BUILD_SHARED_LIBS" \\
      -DCMAKE_INSTALL_PREFIX="$PREFIX_DIR" \\
      -DCMAKE_C_FLAGS="$CFLAGS" \\
      -DCMAKE_CXX_FLAGS="$CXXFLAGS" \\
      -DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS" \\
      -DCMAKE_SHARED_LINKER_FLAGS="$LDFLAGS" \\
      -DCMAKE_MODULE_LINKER_FLAGS="$LDFLAGS" \\
      $EXTRA_CMAKE_FLAGS
    EOF

  - time cmake
    -B Build
    -DREPRODUCIBLE_BUILDS=ON
    -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE"
    -DBUILD_SHARED_LIBS="$BUILD_SHARED_LIBS"
    -DCMAKE_INSTALL_PREFIX="$PREFIX_DIR"
    -DCMAKE_C_FLAGS="$CFLAGS"
    -DCMAKE_CXX_FLAGS="$CXXFLAGS"
    -DCMAKE_EXE_LINKER_FLAGS="$LDFLAGS"
    -DCMAKE_SHARED_LINKER_FLAGS="$LDFLAGS"
    -DCMAKE_MODULE_LINKER_FLAGS="$LDFLAGS"
    $EXTRA_CMAKE_FLAGS || {
      cat Build/CMakeFiles/CMakeError.log;
      exit 1;
    }
  - time cmake --build Build --config "$CMAKE_BUILD_TYPE" ${TARGET:+--target $TARGET}
  - ccache -s

.linux-extract-videos:
  - cp /*.zst .
  - zstd -d *.zst

.enc-test-parallel:
  matrix:
    - SVT_ENCTEST_FILENAME: akiyo_cif.y4m
      SVT_ENCTEST_BITNESS: 8
    - SVT_ENCTEST_FILENAME: Chimera-Aerial_480x264_2997fps_10bit_420_150frames.y4m
      SVT_ENCTEST_BITNESS: 10

.enc-test-artifacts:
  untracked: false
  expire_in: 1 days
  paths:
    - "*.ivf"

.enc-test-script: |
  for CMAKE_BUILD_TYPE in Debug Release; do
    test -e "./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp" || continue
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp --preset 2 -i "$SVT_ENCTEST_FILENAME" -n 17 -b "test-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m2.ivf"
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp --preset 8 -i "$SVT_ENCTEST_FILENAME" -n 120 -b "test-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m8.ivf"
  done

.enc-ext-test-script: |
  for CMAKE_BUILD_TYPE in Debug Release; do
    test -e "./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp" || continue

    # 1-pass VBR
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp -i "$SVT_ENCTEST_FILENAME" --rc 1 --tbr 100 --lp 1 -b "vbr1pass-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m8.ivf" --preset 8
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp -i "$SVT_ENCTEST_FILENAME" --rc 1 --tbr 100 --lp 1 -b "vbr1pass-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m12.ivf" --preset 12

    # 2-pass VBR
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp -i "$SVT_ENCTEST_FILENAME" --rc 1 --tbr 100 --lp 1 -b "vbr2pass-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m8.ivf" --passes 2 --preset 8
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp -i "$SVT_ENCTEST_FILENAME" --rc 1 --tbr 100 --lp 1 -b "vbr2pass-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m12.ivf" --passes 2 --preset 12

    # 1-pass CBR
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp -i "$SVT_ENCTEST_FILENAME" --rc 2 --tbr 100 --lp 1 -b "cbr-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m8.ivf" --pred-struct 1 --preset 8
    ./Bin/$CMAKE_BUILD_TYPE/SvtAv1EncApp -i "$SVT_ENCTEST_FILENAME" --rc 2 --tbr 100 --lp 1 -b "cbr-${BRANCH:-pr}-$(uname -sm | tr ' ' _)-${CMAKE_BUILD_TYPE}-${SVT_ENCTEST_BITNESS}bit-m12.ivf" --pred-struct 1 --preset 12
  done

.ffmpeg-before-script-git-setup:
  - time git config --global user.email "ci@gitlab.com"
  - time git config --global user.name "GitLab CI"

.ffmpeg-before-script-clone:
  - time git clone $PWD svtav1-src
  - git clone https://aomedia.googlesource.com/aom aom-src &
  - git clone https://chromium.googlesource.com/webm/libvpx libvpx-src &
  - git clone https://code.videolan.org/videolan/dav1d.git dav1d-src &
  - git clone https://github.com/Netflix/vmaf.git vmaf-src &
  - git clone https://github.com/FFmpeg/FFmpeg.git ffmpeg-src &
  - time wait

.ffmpeg-before-script-clone-windows:
  - time git clone $PWD svtav1-src
  - time git clone https://aomedia.googlesource.com/aom aom-src
  - time git clone https://chromium.googlesource.com/webm/libvpx libvpx-src
  - time git clone https://code.videolan.org/videolan/dav1d.git dav1d-src
  - time git clone https://github.com/Netflix/vmaf.git vmaf-src
  - time git clone https://github.com/FFmpeg/FFmpeg.git ffmpeg-src

.ffmpeg-before-script-export:
  - true "${CMAKE_BUILD_TYPE:=Release}" "${BUILD_SHARED_LIBS:=OFF}" "${PREFIX_DIR:=/usr/local}"
  - export PKG_CONFIG_PATH=$PREFIX_DIR/lib/pkgconfig${PKG_CONFIG_PATH:+:PKG_CONFIG_PATH}
  - export PATH="${PREFIX_DIR}/bin:${PATH}"

# SVT-AV1
.ffmpeg-svtav1-script:
  - time cmake
    -S svtav1-src
    -B svtav1-build
    -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE"
    -DBUILD_SHARED_LIBS="$BUILD_SHARED_LIBS"
    -DCMAKE_INSTALL_PREFIX="$PREFIX_DIR"
    -DBUILD_APPS=OFF
    -DREPRODUCIBLE_BUILDS=ON
  - time cmake --build svtav1-build --config Release --target install
  - FFMPEG_CONFIG_FLAGS="${FFMPEG_CONFIG_FLAGS:+$FFMPEG_CONFIG_FLAGS }--enable-libsvtav1"

# aom
.ffmpeg-aom-script:
  - time cmake
    -S aom-src
    -B aom-build
    -DCMAKE_BUILD_TYPE="$CMAKE_BUILD_TYPE"
    -DBUILD_SHARED_LIBS="$BUILD_SHARED_LIBS"
    -DCMAKE_INSTALL_PREFIX="$PREFIX_DIR"
    -DENABLE_TESTS=0
    -DENABLE_EXAMPLES=0
    -DENABLE_DOCS=0
    -DENABLE_TESTDATA=0
    -DENABLE_TOOLS=0
  - time cmake --build aom-build --config Release --target install
  - FFMPEG_CONFIG_FLAGS="${FFMPEG_CONFIG_FLAGS:+$FFMPEG_CONFIG_FLAGS }--enable-libaom"
# libvpx
.ffmpeg-libvpx-script:
  - mkdir vpx-build
  - cd vpx-build
  - time dash ../libvpx-src/configure
    --disable-dependency-tracking
    --disable-docs
    --disable-examples
    --disable-libyuv
    --disable-postproc
    --disable-shared
    --disable-tools
    --disable-unit-tests
    --disable-webm-io
    --enable-postproc
    --enable-runtime-cpu-detect
    --enable-vp8 --enable-vp9
    --enable-vp9-highbitdepth
    --enable-vp9-postproc
    --prefix="${PREFIX_DIR}"
  - time make -j $(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu) install
  - cd -
  - FFMPEG_CONFIG_FLAGS="${FFMPEG_CONFIG_FLAGS:+$FFMPEG_CONFIG_FLAGS }--enable-libvpx"
# dav1d
.ffmpeg-dav1d-script:
  - time meson setup
    --default-library static
    --buildtype release
    --libdir lib
    --prefix "${PREFIX_DIR}"
    -Denable_tests=false
    -Denable_examples=false
    -Denable_tools=false
    dav1d-build dav1d-src
  - time meson install -C dav1d-build
  - FFMPEG_CONFIG_FLAGS="${FFMPEG_CONFIG_FLAGS:+$FFMPEG_CONFIG_FLAGS }--enable-libdav1d"
# vmaf
.ffmpeg-vmaf-script:
  - time meson setup
    --default-library static
    --buildtype release
    --libdir lib
    --prefix "${PREFIX_DIR}"
    -Denable_tests=false
    -Denable_docs=false
    -Dbuilt_in_models=true
    -Denable_float=true
    vmaf-build vmaf-src/libvmaf
  - time meson install -C vmaf-build
  - FFMPEG_CONFIG_FLAGS="${FFMPEG_CONFIG_FLAGS:+$FFMPEG_CONFIG_FLAGS }--enable-libvmaf"
# FFmpeg
# Uses ld=CXX for libvmaf to autolink the stdc++ library
.ffmpeg-ffmpeg-script:
  - mkdir ffmpeg-build
  - cd ffmpeg-build
  - time dash ../ffmpeg-src/configure
    --arch=x86_64
    --pkg-config-flags="--static"
    --cc="${CC:-ccache gcc}"
    --cxx="${CXX:-ccache g++}"
    --ld="${CXX:-ccache g++}"
    --enable-gpl --enable-static
    --disable-doc
    --prefix="${PREFIX_DIR}"
    ${FFMPEG_CONFIG_FLAGS}
    --disable-shared || { less ffbuild/config.log; exit 1; }
  - time make -j $(getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu) install
  - cp ./ffmpeg $CI_PROJECT_DIR

# Extends bases

.common-ci-base:
  rules:
    - !reference [.source_code_rules]
  variables: *compiler-variables
