name: CI

on: [push, pull_request]

env:
  CC: clang
  CXX: clang++

jobs:
  pre-commit:
    runs-on: ubuntu-latest-amd64
    steps:
      - uses: actions/checkout@v4

      - uses: actions/cache@v4
        with:
          path: /var/cache/apt/archives
          key: apt-amd64-${{ hashFiles('.gitea/workflows/ci.yml') }}

      - name: Install dependencies
        run: |
          . /etc/os-release
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          echo "deb http://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-21 main" | sudo tee /etc/apt/sources.list.d/llvm.list
          sudo apt-get update -qq
          sudo apt-get install -y \
            clang-21 git nodejs pre-commit
          for tool in clang clang++; do
            sudo update-alternatives --install /usr/bin/${tool} ${tool} /usr/bin/${tool}-21 100
          done

      - uses: actions/cache@v4
        with:
          path: ~/.cache/pre-commit
          key: pre-commit-${{ hashFiles('.pre-commit-config.yaml') }}

      - name: Run pre-commit
        run: |
          git config --global --add safe.directory "$PWD"
          pre-commit run --all-files --show-diff-on-failure

  test:
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: 64-bit-versions
            cmake_args: -DCMAKE_CXX_FLAGS=-DUSE_64_BIT=1
          - name: debug
            cmake_args: -DCMAKE_BUILD_TYPE=Debug
          - name: simd-fallback
            cmake_args: -DUSE_SIMD_FALLBACK=ON
          - name: gcc
            cmake_args: -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++
    runs-on: ubuntu-latest-amd64
    steps:
      - uses: actions/checkout@v4

      - uses: actions/cache@v4
        with:
          path: /var/cache/apt/archives
          key: apt-amd64-${{ hashFiles('.gitea/workflows/ci.yml') }}

      - name: Install dependencies
        run: |
          . /etc/os-release
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          echo "deb http://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-21 main" | sudo tee /etc/apt/sources.list.d/llvm.list
          sudo apt-get update -qq
          sudo apt-get install -y \
            build-essential ccache clang-21 cmake gcc g++ \
            libc6-dbg llvm-21 lld-21 mold ninja-build python3 valgrind zstd
          sudo curl -Ls "https://dl.min.io/client/mc/release/linux-amd64/mc" \
            -o /usr/local/bin/mc && sudo chmod +x /usr/local/bin/mc
          for tool in clang clang++ llvm-ar llvm-nm llvm-ranlib llvm-objcopy llvm-cov llvm-symbolizer lld ld.lld; do
            sudo update-alternatives --install /usr/bin/${tool} ${tool} /usr/bin/${tool}-21 100
          done

      - uses: actions/cache@v4
        with:
          path: .ccache
          key: ccache-${{ matrix.name }}-${{ gitea.sha }}
          restore-keys: |
            ccache-${{ matrix.name }}-

      - name: Build
        run: |
          export CCACHE_DIR="$GITHUB_WORKSPACE/.ccache"
          rm -rf build
          cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache ${{ matrix.cmake_args }}
          ninja -C build
          ccache -s

      - name: Test
        run: |
          cd build
          ctest --no-compress-output --test-output-size-passed 100000 --test-output-size-failed 100000 -T Test -j "$(nproc)" --timeout 90 > /dev/null

      - name: Upload test results to MinIO
        if: always()
        env:
          MINIO_ACCESS_KEY: ${{ secrets.MINIO_ACCESS_KEY }}
          MC_HOST_minio: https://${{ secrets.MINIO_ACCESS_KEY }}:${{ secrets.MINIO_SECRET_KEY }}@minio.weaselab.dev
        run: |
          if [ -z "$MINIO_ACCESS_KEY" ]; then
            echo "MinIO credentials not configured; skipping upload"
            exit 0
          fi
          zstd build/Testing/*/Test.xml
          mc cp build/Testing/*/Test.xml.zst "minio/jenkins/conflict-set/${{ gitea.run_number }}/${{ matrix.name }}/"

      - name: Test summary
        if: always()
        run: |
          python3 ctest_summary.py build/Testing/*/Test.xml \
            --link "https://minio.weaselab.dev/jenkins/conflict-set/${{ gitea.run_number }}/${{ matrix.name }}/Test.xml.zst" \
            | tee -a "$GITHUB_STEP_SUMMARY"

  release:
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: ubuntu-latest-amd64
            arch: amd64
          - runner: ubuntu-latest-arm64
            arch: arm64
    runs-on: ${{ matrix.runner }}
    steps:
      - uses: actions/checkout@v4

      - uses: actions/cache@v4
        with:
          path: /var/cache/apt/archives
          key: apt-${{ matrix.arch }}-${{ hashFiles('.gitea/workflows/ci.yml') }}

      - name: Install dependencies
        run: |
          . /etc/os-release
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          echo "deb http://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-21 main" | sudo tee /etc/apt/sources.list.d/llvm.list
          sudo apt-get update -qq
          sudo apt-get install -y \
            biber build-essential ccache clang-21 cmake devscripts \
            latexmk libc6-dbg llvm-21 lld-21 mold ninja-build rpm \
            texlive-bibtex-extra texlive-fonts-recommended \
            texlive-latex-extra texlive-pictures valgrind zstd
          sudo curl -Ls "https://dl.min.io/client/mc/release/linux-$(dpkg --print-architecture)/mc" \
            -o /usr/local/bin/mc && sudo chmod +x /usr/local/bin/mc
          for tool in clang clang++ llvm-ar llvm-nm llvm-ranlib llvm-objcopy llvm-cov llvm-symbolizer lld ld.lld; do
            sudo update-alternatives --install /usr/bin/${tool} ${tool} /usr/bin/${tool}-21 100
          done

      - uses: actions/cache@v4
        with:
          path: .ccache
          key: ccache-release-${{ matrix.arch }}-${{ gitea.sha }}
          restore-keys: |
            ccache-release-${{ matrix.arch }}-

      - name: Build
        run: |
          export CCACHE_DIR="$GITHUB_WORKSPACE/.ccache"
          rm -rf build
          cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_FLAGS=-DNVALGRIND
          ninja -C build
          ccache -s

      - name: Test
        run: |
          cd build
          # On arm64, valgrind needs the MAKE_MEM_DEFINED client requests for
          # https://git.weaselab.dev/weaselab/conflict-set/issues/39, but this
          # build has -DNVALGRIND, which compiles them out. Skip valgrind
          # tests here; they run annotated in the test job.
          ctest --no-compress-output --test-output-size-passed 100000 --test-output-size-failed 100000 ${{ matrix.arch == 'arm64' && '-E valgrind' || '' }} -T Test -j "$(nproc)" --timeout 90 > /dev/null

      - name: Package
        run: |
          cd build
          cpack -G DEB
          cpack -G RPM

      - name: Build paper
        if: matrix.arch == 'amd64'
        run: |
          cd paper
          make

      - name: Upload artifacts to MinIO
        if: always()
        shell: bash
        env:
          MINIO_ACCESS_KEY: ${{ secrets.MINIO_ACCESS_KEY }}
          MC_HOST_minio: https://${{ secrets.MINIO_ACCESS_KEY }}:${{ secrets.MINIO_SECRET_KEY }}@minio.weaselab.dev
        run: |
          if [ -z "$MINIO_ACCESS_KEY" ]; then
            echo "MinIO credentials not configured; skipping upload"
            exit 0
          fi
          dest="minio/jenkins/conflict-set/${{ gitea.run_number }}/release-${{ matrix.arch }}/"
          zstd build/Testing/*/Test.xml
          mc cp build/Testing/*/Test.xml.zst "$dest"
          if compgen -G "build/*.deb" > /dev/null; then
            mc cp build/*.deb "$dest"
          fi
          if compgen -G "build/*.rpm" > /dev/null; then
            mc cp build/*.rpm "$dest"
          fi
          if compgen -G "paper/*.pdf" > /dev/null; then
            mc cp paper/*.pdf "$dest"
          fi

      - name: Test summary
        if: always()
        run: |
          python3 ctest_summary.py build/Testing/*/Test.xml \
            --link "https://minio.weaselab.dev/jenkins/conflict-set/${{ gitea.run_number }}/release-${{ matrix.arch }}/Test.xml.zst" \
            | tee -a "$GITHUB_STEP_SUMMARY"

  coverage:
    runs-on: ubuntu-latest-amd64
    steps:
      - uses: actions/checkout@v4

      - uses: actions/cache@v4
        with:
          path: /var/cache/apt/archives
          key: apt-amd64-${{ hashFiles('.gitea/workflows/ci.yml') }}

      - name: Install dependencies
        run: |
          . /etc/os-release
          wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
          echo "deb http://apt.llvm.org/${VERSION_CODENAME}/ llvm-toolchain-${VERSION_CODENAME}-21 main" | sudo tee /etc/apt/sources.list.d/llvm.list
          sudo apt-get update -qq
          sudo apt-get install -y \
            build-essential ccache clang-21 cmake gcovr \
            libc6-dbg llvm-21 lld-21 mold ninja-build python3 valgrind zstd
          sudo curl -Ls "https://dl.min.io/client/mc/release/linux-amd64/mc" \
            -o /usr/local/bin/mc && sudo chmod +x /usr/local/bin/mc
          for tool in clang clang++ llvm-ar llvm-nm llvm-ranlib llvm-objcopy llvm-cov llvm-symbolizer lld ld.lld; do
            sudo update-alternatives --install /usr/bin/${tool} ${tool} /usr/bin/${tool}-21 100
          done

      - uses: actions/cache@v4
        with:
          path: .ccache
          key: ccache-coverage-${{ gitea.sha }}
          restore-keys: |
            ccache-coverage-

      - name: Build
        run: |
          export CCACHE_DIR="$GITHUB_WORKSPACE/.ccache"
          rm -rf build
          cmake -S . -B build -G Ninja -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_C_FLAGS=--coverage -DCMAKE_CXX_FLAGS=--coverage \
            -DCMAKE_BUILD_TYPE=Debug -DDISABLE_TSAN=ON
          ninja -C build
          ccache -s

      - name: Test
        run: |
          cd build
          ctest --no-compress-output --test-output-size-passed 100000 --test-output-size-failed 100000 -T Test -j "$(nproc)" --timeout 90 > /dev/null

      - name: Coverage report
        shell: bash
        run: |
          gcov_args=(-f ConflictSet.cpp -f LongestCommonPrefix.h -f Metrics.h
                     --gcov-executable "llvm-cov gcov" --exclude-noncode-lines)
          gcovr "${gcov_args[@]}" --cobertura > build/coverage.xml
          gcovr "${gcov_args[@]}"
          mkdir -p build/coverage_html
          gcovr "${gcov_args[@]}" --html-details build/coverage_html/index.html
          gcovr "${gcov_args[@]}" --fail-under-line 100 > /dev/null

      - name: Upload results to MinIO
        if: always()
        env:
          MINIO_ACCESS_KEY: ${{ secrets.MINIO_ACCESS_KEY }}
          MC_HOST_minio: https://${{ secrets.MINIO_ACCESS_KEY }}:${{ secrets.MINIO_SECRET_KEY }}@minio.weaselab.dev
        run: |
          if [ -z "$MINIO_ACCESS_KEY" ]; then
            echo "MinIO credentials not configured; skipping upload"
            exit 0
          fi
          dest="minio/jenkins/conflict-set/${{ gitea.run_number }}/coverage/"
          zstd build/Testing/*/Test.xml
          mc cp build/Testing/*/Test.xml.zst "$dest"
          if [ -e build/coverage.xml ]; then
            mc cp build/coverage.xml "$dest"
          fi
          if [ -d build/coverage_html ]; then
            mc cp -r build/coverage_html "$dest"
          fi

      - name: Test summary
        if: always()
        run: |
          python3 ctest_summary.py build/Testing/*/Test.xml \
            --link "https://minio.weaselab.dev/jenkins/conflict-set/${{ gitea.run_number }}/coverage/Test.xml.zst" \
            | tee -a "$GITHUB_STEP_SUMMARY"
          echo "" | tee -a "$GITHUB_STEP_SUMMARY"
          echo "📊 [Coverage report](https://minio.weaselab.dev/jenkins/conflict-set/${{ gitea.run_number }}/coverage/coverage_html/index.html)" | tee -a "$GITHUB_STEP_SUMMARY"
