
name: include-what-you-use

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

permissions:
  contents: read

jobs:
  iwyu:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        include:
          - cmake_options: all-options-abiv1
            warning_limit: 161
          - cmake_options: all-options-abiv1-preview
            warning_limit: 190
          - cmake_options: all-options-abiv2-preview
            warning_limit: 187

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

      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: 'recursive'

      - name: setup dependencies
        run: |
          sudo apt update -y
          wget -qO- https://apt.llvm.org/llvm.sh | sudo bash -s -- 22
          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-22-dev \
            libclang-22-dev \
            clang-22 \
            cmake
          sudo update-alternatives --install /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-22 200

      - name: Install rapidyaml
        run: |
          sudo -E ./ci/install_thirdparty.sh --install-dir /usr/local --tags-file third_party_release --packages "ryml"

      - name: Install include-what-you-use
        run: |
          sudo ./ci/install_iwyu.sh

      - name: Build with include-what-you-use
        env:
          OTELCPP_CMAKE_CACHE_FILE: ${{ matrix.cmake_options }}.cmake
          BUILD_DIR: build-${{ matrix.cmake_options }}
        run: |
          ./ci/do_ci.sh cmake.iwyu.test
          cp "${BUILD_DIR}/opentelemetry-cpp-iwyu-${{ matrix.cmake_options }}.log" 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
