# Copyright (c) V-Nova International Limited 2024-2026. All rights reserved.
# This software is licensed under the BSD-3-Clause-Clear License by V-Nova Limited.
# No patent licenses are granted under this license. For enquiries about patent licenses,
# please contact legal@v-nova.com.
# The LCEVCdec software is a stand-alone project and is NOT A CONTRIBUTION to any other project.
# If the software is incorporated into another project, THE TERMS OF THE BSD-3-CLAUSE-CLEAR LICENSE
# AND THE ADDITIONAL LICENSING INFORMATION CONTAINED IN THIS FILE MUST BE MAINTAINED, AND THE
# SOFTWARE DOES NOT AND MUST NOT ADOPT THE LICENSE OF THE INCORPORATING PROJECT. However, the
# software may be incorporated into a project under a compatible license provided the requirements
# of the BSD-3-Clause-Clear license are respected, and V-Nova Limited remains
# licensor of the software ONLY UNDER the BSD-3-Clause-Clear license (not the compatible license).
# ANY ONWARD DISTRIBUTION, WHETHER STAND-ALONE OR AS PART OF ANY OTHER PROJECT, REMAINS SUBJECT TO
# THE EXCLUSION OF PATENT LICENSES PROVISION OF THE BSD-3-CLAUSE-CLEAR LICENSE.

name: CI Linux

on:
  pull_request:

env:
  BUILD_TYPE: Release

permissions: {}

jobs:
  lint:
    name: Lint
    runs-on: ubuntu-22.04
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0
          persist-credentials: false

      - name: Fetch target repo refs
        run: git remote add target https://github.com/v-novaltd/LCEVCdec.git && git fetch target

      - name: Install required lib
        run: pip3 install -r cmake/tools/lint_requirements.txt

      - name: Run Lint Check
        env:
          SOURCE_BRANCH: HEAD
        run: export TARGET_BRANCH=$(git merge-base HEAD target/main) && python3 cmake/tools/lint.py --check-only --diff-main

  build:
    name: Build
    needs: lint
    runs-on: ubuntu-22.04
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0
          persist-credentials: false

      - name: Update System Packages
        run: sudo apt-get update

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@acb598e5ddbc6f68a970c5da0688d2f3a9f04d05 # v1.6.0
        with:
          packages: nlohmann-json3-dev ninja-build libfmt-dev libcli11-dev libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libgtest-dev libgmock-dev libxxhash-dev librange-v3-dev libvulkan-dev glslang-tools unzip

      - name: ccache
        uses: hendrikmuhs/ccache-action@5ebbd400eff9e74630f759d94ddd7b6c26299639 # v1.2
        with:
          key: ccache-${{ github.ref_name }}
          append-timestamp: false

      - name: Cmake Build
        run: |
          export PATH="/usr/lib/ccache:$PATH"
          mkdir build && cd build
          cmake -G Ninja -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DVN_SDK_PIPELINE_VULKAN=ON -DVN_SDK_EXECUTABLES=ON -DVN_SDK_UNIT_TESTS=ON -DVN_SDK_WARNINGS_FAIL=ON ..
          cmake --build .

      - name: Cmake Install
        run: cmake --install build --prefix "${GITHUB_WORKSPACE}"/install

      - name: Create Build Archive
        run: tar -czf install.tar.gz -C "${GITHUB_WORKSPACE}" install

      - name: Upload Build Artifacts
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
        with:
          name: install-artifacts
          path: install.tar.gz

  unit_tests:
    name: Unit tests
    needs: build
    runs-on: ubuntu-22.04
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0
          persist-credentials: false

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@acb598e5ddbc6f68a970c5da0688d2f3a9f04d05 # v1.6.0
        with:
          packages: nlohmann-json3-dev ninja-build libfmt-dev libcli11-dev libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libgtest-dev libgmock-dev libxxhash-dev librange-v3-dev libvulkan-dev glslang-tools unzip

      - name: Download Build Artifacts
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: install-artifacts

      - name: Extract Build Archive
        run: tar -xzf install.tar.gz

      - name: Run Unit Tests
        run: |
          export LD_LIBRARY_PATH=$GITHUB_WORKSPACE/install/lib
          status=0
          for test in install/bin/*test_unit; do
            echo "Running test: ${test}"
            if ! "${test}"; then
              echo "Unit Test failed: ${test}"
              status=1
              continue
            fi
          done
          exit "${status}"

  func_tests:
    name: Functional Tests
    needs: unit_tests
    runs-on: ubuntu-22.04
    permissions:
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}
          fetch-depth: 0
          persist-credentials: false

      - name: Cache APT Packages
        uses: awalsh128/cache-apt-pkgs-action@acb598e5ddbc6f68a970c5da0688d2f3a9f04d05 # v1.6.0
        with:
          packages: nlohmann-json3-dev ninja-build libfmt-dev libcli11-dev libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libgtest-dev libgmock-dev libxxhash-dev librange-v3-dev libvulkan-dev glslang-tools unzip

      - name: Download Build Artifacts
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: install-artifacts

      - name: Extract Build Archive
        run: tar -xzf install.tar.gz

      - name: Create URLs
        run: |
          echo "https://nbg1.your-objectstorage.com/lcevcdec/LCEVCdec_$(git describe --match "[0-9].[0-9].[0-9]" --abbrev=0)_external_test_assets.zip" > url.txt

      - name: Cache External Assets
        id: cache-assets
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
        with:
          path: test_cache/
          key: test-assets-${{ hashFiles('url.txt') }}

      - name: Download External Assets
        if: steps.cache-assets.outputs.cache-hit != 'true'
        run: |
          mkdir -p test_cache
          echo "$(git describe --match "[0-9].[0-9].[0-9]" --abbrev=0)-External" > test_cache/version.txt
          curl -L -o test_cache.zip "$(cat url.txt)"

      - name: Unzip Test Assets
        run: unzip test_cache.zip

      - name: Install required lib
        run: pip3 install -r src/func_tests/test_requirements.txt

      - name: Run Functional Tests
        run: |
          export PLATFORM=External
          export BIN_DIR=install/bin
          export LEVEL=MR
          if ! python3 src/func_tests/run_tests.py; then
            echo "Function test failed, see error in pipeline"
              exit 1
          fi
