name: Daily Coverage

on:
    schedule:
      - cron: '0 6 * * *' # run at 6 AM UTC
    workflow_dispatch:

jobs:
  build:
    # The CMake configure and build commands are platform agnostic and should work equally
    # well on Windows or Mac.  You can convert this to a matrix build if you need
    # cross-platform coverage.
    # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - container: "ubuntu-dev:20"
            build-type: Release
            compiler: {cxx: g++, c: gcc}
            cxx_flags: "-fprofile-arcs -ftest-coverage"
    timeout-minutes: 60
    container:
      image: ghcr.io/romange/${{ matrix.container }}
      credentials:
        username: ${{ github.repository_owner }}
        password: ${{ secrets.GITHUB_TOKEN }}
      volumes:
        - /:/hostroot

    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Delete Space
        run: |
          df -h
          rm -rf /hostroot/usr/share/dotnet
          rm -rf /hostroot/usr/local/share/boost
          rm -rf /hostroot/usr/local/lib/android
          rm -rf /hostroot/opt/ghc
          echo "***************After Deletion***************************"
          df -h
      - name: Install dependencies
        run: |
          uname -a
          cmake --version
          mkdir -p ${{github.workspace}}/build
          apt update && apt install -y lcov pip
      - name: Cache build deps
        id: cache-deps
        uses: actions/cache@v4
        with:
          path: |
            ~/.ccache
            ${{github.workspace}}/build/_deps
          key: ${{ runner.os }}-deps-${{ github.base_ref }}-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-deps-${{ github.base_ref }}-

      - name: Configure CMake
        run: |
          pip install -r tests/dragonfly/requirements.txt
          cmake -B build \
            -DCMAKE_BUILD_TYPE=${{matrix.build-type}} \
            -GNinja \
            -DCMAKE_C_COMPILER="${{matrix.compiler.c}}" \
            -DCMAKE_CXX_COMPILER="${{matrix.compiler.cxx}}" \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_CXX_FLAGS="${{matrix.cxx_flags}}" \
            -L
          pwd
          cd build && pwd
      - name: Build & Test Coverage
        run: |
          cd $GITHUB_WORKSPACE/build
          ninja

          # Be permissive with errors, we hit compiler bugs from time to time.
          ninja test || true

          export DRAGONFLY_PATH=`pwd`/dragonfly
          pytest ../tests/dragonfly/ || true

          lcov -c -d . -o main_coverage.info
          lcov --remove main_coverage.info -o main_coverage.info '/usr/*' '*/_deps/*' '*/third_party/*'
          genhtml main_coverage.info --ignore-errors source --output-directory covout -p $GITHUB_WORKSPACE
          ls ./
          echo ls covout
          ls covout/
      - name: Upload coverage
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: build/covout/
          if-no-files-found: error
