
name: include-what-you-use

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

permissions:
  contents: read

jobs:
  iwyu:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - cmake_options: all-options-abiv1
            warning_limit: 0
          - cmake_options: all-options-abiv1-preview
            warning_limit: 0
          - cmake_options: all-options-abiv2-preview
            warning_limit: 0

    steps:
      - name: Harden the runner (Audit all outbound calls)
        uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
        with:
          egress-policy: audit

      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          submodules: 'recursive'

      - name: setup dependencies
        run: |
          sudo apt update -y
          sudo apt install -y --no-install-recommends --no-install-suggests \
            build-essential \
            ninja-build \
            libssl-dev \
            libcurl4-openssl-dev \
            libabsl-dev \
            libprotobuf-dev \
            libgrpc++-dev \
            protobuf-compiler \
            protobuf-compiler-grpc \
            libgmock-dev \
            libgtest-dev \
            libbenchmark-dev \
            llvm-dev \
            libclang-dev \
            cmake
      - name: Install include-what-you-use
        run: |
          sudo ./ci/install_iwyu.sh
      - name: Prepare CMake
        env:
          CC: clang
          CXX: clang++
        run: |
          TOPDIR=`pwd`
          cmake -B build-${{ matrix.cmake_options }} \
            -C ./test_common/cmake/${{ matrix.cmake_options }}.cmake \
            -DCMAKE_CXX_STANDARD=14 \
            -DWITH_STL=CXX14 \
            -DCMAKE_CXX_FLAGS="-Wno-deprecated-declarations" \
            -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE="include-what-you-use;-w;-Xiwyu;--mapping_file=${TOPDIR}/.iwyu.imp;"

      - name: iwyu_tool
        run: |
          cmake --build build-${{ matrix.cmake_options }} -- -j$(nproc) -k 2>&1 | tee -a iwyu-${{ matrix.cmake_options }}.log

      - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        if: success() || failure()
        with:
          name: Logs-iwyu-${{ matrix.cmake_options }}
          path: ./iwyu-${{ matrix.cmake_options }}.log

      - name: count warnings
        run: |
          set +e
          echo "include-what-you-use version:"
          include-what-you-use --version
          readonly WARNING_COUNT=`grep -c "include-what-you-use reported diagnostics:" iwyu-${{ matrix.cmake_options }}.log`
          echo "include-what-you-use reported ${WARNING_COUNT} warning(s) with cmake options preset '${{ matrix.cmake_options }}'"
          # Acceptable limit, to decrease over time down to 0
          readonly WARNING_LIMIT=${{ matrix.warning_limit }}
          # FAIL the build if WARNING_COUNT > WARNING_LIMIT
          if [ $WARNING_COUNT -gt $WARNING_LIMIT ] ; then
            echo "include-what-you-use reported ${WARNING_COUNT} warning(s) exceeding the existing warning limit of ${WARNING_LIMIT} with cmake options preset '${{ matrix.cmake_options }}'"
            exit 1
          # WARN in annotations if WARNING_COUNT > 0
          elif [ $WARNING_COUNT -gt 0 ] ; then
            echo "::warning::include-what-you-use reported ${WARNING_COUNT} warning(s) with cmake options preset '${{ matrix.cmake_options }}'"
          fi
