stages:
    - style
    - build
    - test

style-check:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: style
    tags:
        - debian
        - amd64
    script:
        - git grep -I -n -P "\t|\r| $" -- . ':(exclude)*/compat/*' && exit 1
        - git grep -I -n -i "david" -- . ':(exclude)THANKS.md' ':(exclude).gitlab-ci.yml' && exit 1
        - git grep -I -l -z "" -- . ':(exclude)*/compat/*' | while IFS= read -r -d '' i; do
              if [ -n "$(tail -c 1 "$i")" ]; then
                  echo "No newline at end of $i";
                  exit 1;
              fi;
          done
        - git remote rm upstream 2> /dev/null || true
        - git remote add upstream https://code.videolan.org/videolan/dav1d.git
        - git fetch -q upstream master
        - for i in $(git rev-list HEAD ^upstream/master); do
              echo "Checking commit message of $i";
              msg="$(git log --format=%B -n 1 $i)";
              if [ -n "$(echo "$msg" | awk "NR==2")" ]; then
                  echo "Malformed commit message in $i, second line must be empty";
                  exit 1;
              fi;
              if echo "$msg" | head -1 | grep -q '\.$'; then
                  echo "Malformed commit message in $i, trailing period in subject line";
                  exit 1;
              fi;
          done

build-debian:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: build
    tags:
        - avx2
        - amd64
    script:
        - meson build --buildtype release --werror
        - ninja -C build
        - cd build && meson test -v

build-debian-static:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: build
    tags:
        - debian
        - amd64
    script:
        - meson build --buildtype release --default-library static --werror
        - ninja -C build
        - cd build && meson test -v
        - nm -A -g src/libdav1d.a | grep " [ABCDGRST] " | (! grep -v " _*dav1d_")

build-debian32:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20181218135732
    stage: build
    tags:
        - debian
        - amd64
    script:
        - meson build --buildtype release
                      --werror
                      --cross-file /opt/crossfiles/linux32.meson
        - ninja -C build
        - cd build && meson test -v

build-win32:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: build
    tags:
        - debian
        - amd64
    script:
        - wineserver -p && wine wineboot
        - meson build --buildtype release
                      --werror
                      --libdir lib
                      --prefix "$(pwd)/build/dav1d_install"
                      --cross-file /opt/crossfiles/i686-w64-mingw32.meson
                      -Ddefault_library=both
        - ninja -C build
        - ninja -C build install
        - cd build && meson test -v
        - i686-w64-mingw32-nm -A -g src/libdav1d.a | grep " [ABCDGRST] " | (! grep -E -v " \.| _*dav1d_")
    artifacts:
        name: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
        paths:
            - build/dav1d_install/
        expire_in: 1 week

build-win32-unaligned-stack:
    image: registry.videolan.org:5000/vlc-debian-llvm-mingw:20190218133533
    stage: build
    tags:
        - debian
        - amd64
    script:
        - wineserver -p && wine wineboot
        - meson build --buildtype release
                      --werror
                      --cross-file /opt/crossfiles/i686-w64-mingw32.meson
                      -Dstack_alignment=4
        - ninja -C build
        - cd build && meson test -v

build-win64:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: build
    tags:
        - debian
        - amd64
    script:
        - wineserver -p && wine wineboot
        - meson build --buildtype release
                      --werror
                      --libdir lib
                      --prefix "$(pwd)/build/dav1d_install"
                      --cross-file /opt/crossfiles/x86_64-w64-mingw32.meson
                      -Ddefault_library=both
        - ninja -C build
        - ninja -C build install
        - cd build && meson test -v
        - x86_64-w64-mingw32-nm -A -g src/libdav1d.a | grep " [ABCDGRST] " | (! grep -E -v " \.| _*dav1d_")
    artifacts:
        name: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
        paths:
            - build/dav1d_install/
        expire_in: 1 week

build-win-arm32:
    image: registry.videolan.org:5000/vlc-debian-llvm-mingw:20190218133533
    stage: build
    tags:
        - debian
        - amd64
    script:
        - meson build --buildtype release
                      --werror
                      --libdir lib
                      --prefix "$(pwd)/build/dav1d_install"
                      --cross-file /opt/crossfiles/armv7-w64-mingw32.meson
                      -Ddefault_library=both
        - ninja -C build
        - armv7-w64-mingw32-nm -A -g build/src/libdav1d.a | grep " [ABCDGRST] " | (! grep -E -v " \.| _*dav1d_")

build-win-arm64:
    image: registry.videolan.org:5000/vlc-debian-llvm-mingw:20190218133533
    stage: build
    tags:
        - debian
        - amd64
    script:
        - meson build --buildtype release
                      --werror
                      --libdir lib
                      --prefix "$(pwd)/build/dav1d_install"
                      --cross-file /opt/crossfiles/aarch64-w64-mingw32.meson
                      -Ddefault_library=both
        - ninja -C build
        - ninja -C build install
        - aarch64-w64-mingw32-nm -A -g build/src/libdav1d.a | grep " [ABCDGRST] " | (! grep -E -v " \.| _*dav1d_")
    artifacts:
        name: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
        paths:
            - build/dav1d_install/
        expire_in: 1 week

build-debian-aarch64:
    stage: build
    image: registry.videolan.org/dav1d-debian-buster-aarch64:20200218203017
    tags:
        - aarch64
        - debian
    script:
        - meson build --buildtype debugoptimized --werror
        - ninja -C build
        - cd build && meson test -v

build-debian-aarch64-clang-5:
    stage: build
    image: registry.videolan.org/dav1d-debian-buster-aarch64:20200218203017
    tags:
        - aarch64
        - debian
    script:
        - env CC=clang-5.0 CFLAGS='-integrated-as' meson build --buildtype release
        - ninja -C build
        - cd build && meson test -v

build-macos:
    stage: build
    tags:
        - macos
    script:
        - meson build --buildtype release -Ddefault_library=both --werror
        - ninja -C build
        - cd build && meson test -v

build-debian-werror:
    image: registry.videolan.org/dav1d-debian-buster-aarch64:20200218203017
    stage: build
    tags:
        - aarch64
        - debian
    script:
        - env CC='clang-7' meson build --buildtype debug --werror
        - ninja -C build

build-debian-armv7:
    stage: build
    image: registry.videolan.org:5000/dav1d-debian-unstable-armv7:20190202101732
    tags:
        - armv7
        - debian
    script:
        - linux32 meson build --buildtype debugoptimized --werror
        - ninja -C build
        - cd build && meson test -v

build-debian-armv7-clang-5:
    stage: build
    image: registry.videolan.org:5000/dav1d-debian-unstable-armv7:20190202101732
    tags:
        - armv7
        - debian
    script:
        - env CC=clang-5.0 CFLAGS='-integrated-as' linux32 meson build --buildtype release
        - ninja -C build
        - cd build && meson test -v

build-ubuntu-snap:
    stage: build
    image: registry.videolan.org/dav1d-ubuntu-bionic:20200121182340
    tags:
        - debian
        - amd64
    script:
        - cd package/snap && snapcraft snap
        - |
           if [ "$CI_PROJECT_NAMESPACE" = "videolan" ]; then
            echo $SNAP_LOGIN | base64 --decode | snapcraft login --with -
            snapcraft push dav1d_*.snap --release edge
            snapcraft logout
           fi
    artifacts:
        name: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG"
        paths:
            - package/snap/dav1d_*.snap
        expire_in: 1 week
    allow_failure: true

build-debian-ppc64le:
    stage: build
    image: registry.videolan.org/dav1d-debian-unstable-ppc64le:20190606105121
    tags:
        - ppc64le
        - docker
    script:
        - meson build --buildtype release --werror
        - ninja -C build
        - cd build && meson test -v

test-debian:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: test
    needs: ["build-debian"]
    tags:
        - debian
        - amd64
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - meson build --buildtype release -Dtestdata_tests=true -Dlogging=false
        - ninja -C build
        - cd build && time meson test -v
    dependencies: []

test-debian-unaligned-stack:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: test
    needs: ["build-debian"]
    tags:
        - avx2
        - amd64
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - meson build --buildtype release -Dtestdata_tests=true -Dlogging=false -Dstack_alignment=16
        - ninja -C build
        - cd build && time meson test -v
    dependencies: []

test-debian-asan:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: test
    needs: ["build-debian"]
    tags:
        - debian
        - amd64
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    variables:
        ASAN_OPTIONS: 'detect_leaks=0'
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - meson build --buildtype debugoptimized -Dtestdata_tests=true -Dlogging=false -Db_sanitize=address -Denable_asm=false
        - ninja -C build
        - cd build && time meson test -v --setup=sanitizer
    dependencies: []

test-debian-msan:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: test
    needs: ["build-debian"]
    tags:
        - debian
        - amd64
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    variables:
        MSAN_OPTIONS: 'exitcode=1'
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - env CC=clang meson build --buildtype debugoptimized -Dtestdata_tests=true -Dlogging=false -Db_sanitize=memory -Db_lundef=false -Denable_asm=false
        - ninja -C build
        - cd build && time meson test -v --setup=sanitizer
    dependencies: []

test-debian-ubsan:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: test
    needs: ["build-debian"]
    tags:
        - debian
        - amd64
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    variables:
        UBSAN_OPTIONS: 'print_stacktrace=1:halt_on_error=1'
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - env CC=clang meson build --buildtype debugoptimized -Dtestdata_tests=true -Dlogging=false -Db_sanitize=undefined -Db_lundef=false -Denable_asm=false
        - ninja -C build
        - cd build && time meson test -v --setup=sanitizer
    dependencies: []

test-win64:
    image: registry.videolan.org:5000/dav1d-debian-unstable:20190215130514
    stage: test
    needs: ["build-win64"]
    tags:
        - avx2
        - amd64
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - wineserver -p && wine wineboot
        - meson build --buildtype release
                      -Dtestdata_tests=true
                      -Dlogging=false
                      --cross-file /opt/crossfiles/x86_64-w64-mingw32.meson
        - ninja -C build
        - cd build && time meson test -v
    dependencies: []

test-debian-aarch64:
    image: registry.videolan.org/dav1d-debian-buster-aarch64:20200218203017
    stage: test
    needs: ["build-debian-aarch64"]
    tags:
        - aarch64
        - debian
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - meson build --buildtype release
                      -Dtestdata_tests=true
                      -Dlogging=false
        - ninja -C build
        - cd build && time meson test -v
    dependencies: []

test-debian-ppc64le:
    image: registry.videolan.org/dav1d-debian-unstable-ppc64le:20190606105121
    stage: test
    needs: ["build-debian-ppc64le"]
    tags:
        - ppc64le
        - docker
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - meson build --buildtype release
                      -Dtestdata_tests=true
                      -Dlogging=false
        - ninja -C build
        - cd build && time meson test -v
    dependencies: []

test-debian-armv7-clang-5:
    stage: test
    image: registry.videolan.org:5000/dav1d-debian-unstable-armv7:20190202101732
    needs: ["build-debian-armv7-clang-5"]
    tags:
        - armv7
        - debian
    cache:
        key: testdata.git-20190215
        paths:
            - cache/dav1d-test-data.git/
    script:
        - test -d cache || mkdir cache
        - test -d cache/dav1d-test-data.git && GIT_DIR=cache/dav1d-test-data.git git fetch --refmap=refs/heads/master:refs/heads/master origin master
        - test -d cache/dav1d-test-data.git || git clone --bare https://code.videolan.org/videolan/dav1d-test-data.git cache/dav1d-test-data.git
        - git clone cache/dav1d-test-data.git tests/dav1d-test-data
        - env CC=clang-5.0 CFLAGS='-integrated-as' linux32 meson build --buildtype release
                                                               -Dtestdata_tests=true
                                                               -Dlogging=false
        - ninja -C build
        - cd build && time meson test -v
    dependencies: []
