name: iwyu

on:
  pull_request:
    branches:
      - master

env:
  BUILD_TYPE: RelWithDebInfo
  BUILD_DIR: build
  CLEANER_OUTPUT_PATH: build/clang-include-cleaner.log
  # the "idl" subdirectory does not contain C++ source code. the .hh files in it are
  # supposed to be processed by idl-compiler.py, so we don't check them using the cleaner
  CLEANER_DIRS: test/unit exceptions alternator api auth cdc compaction db dht gms index lang message mutation mutation_writer node_ops redis replica

permissions: {}

# cancel the in-progress run upon a repush
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  read-toolchain:
    uses: ./.github/workflows/read-toolchain.yaml
  clang-include-cleaner:
    name: "Analyze #includes in source files"
    needs:
      - read-toolchain
    runs-on: ubuntu-latest
    container: ${{ needs.read-toolchain.outputs.image }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - run: |
          sudo dnf -y install clang-tools-extra
      - name: Generate compilation database
        run: |
          cmake                                         \
            -DCMAKE_BUILD_TYPE=$BUILD_TYPE              \
            -DCMAKE_C_COMPILER=clang                    \
            -DCMAKE_CXX_COMPILER=clang++                \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON          \
            -G Ninja                                    \
            -B $BUILD_DIR                               \
            -S .
      - run: |
          cmake                       \
          --build $BUILD_DIR          \
          --target wasmtime_bindings
      - name: Build headers
        run: |
          swagger_targets=''
          for f in api/api-doc/*.json; do
            if test "${f#*.}" = json; then
              name=$(basename "$f" .json)
              if test $name != swagger20_header; then
                swagger_targets+=" scylla_swagger_gen_$name"
              fi
            fi
          done
          cmake                                         \
            --build build                               \
             --target seastar_http_request_parser       \
             --target idl-sources                       \
             --target $swagger_targets
      - run: |
          echo "::add-matcher::.github/clang-include-cleaner.json"
      - name: clang-include-cleaner
        run: |
          for d in $CLEANER_DIRS; do
            find $d -name '*.cc' -o -name '*.hh'          \
              -exec echo {} \;                            \
              -exec clang-include-cleaner                 \
                --ignore-headers=seastarx.hh              \
                --print=changes                           \
                -p $BUILD_DIR                             \
                {} \; | tee --append $CLEANER_OUTPUT_PATH
          done
      - run: |
          echo "::remove-matcher owner=clang-include-cleaner::"
      - uses: actions/upload-artifact@v4
        with:
          name: Logs (clang-include-cleaner)
          path: "./${{ env.CLEANER_OUTPUT_PATH }}"
