# Syntax reference https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
# Environment reference https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners
name: include-what-you-use

on:
  schedule:
    - cron: '0 0 * * 0'
  workflow_dispatch:

permissions:
  contents: read

jobs:
  iwyu:

    strategy:
      matrix:
        # "opensuse/tumbleweed:latest" / "fedora:latest" / "debian:unstable" / "archlinux:latest"
        include:
          - os: ubuntu-22.04
            image: "fedora:latest"
            stdlib: libstdc++
            clang_inc: '-isystem/usr/lib/clang/20/include'
          # TODO: disable because it currently fails with "error: <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header."
          #- os: ubuntu-22.04
          #  image: "fedora:latest"
          #  stdlib: libc++
          #  clang_inc: '-isystem/usr/lib/clang/20/include'
          - os: macos-26
            image: ""
            stdlib: libc++  # no libstdc++ on macOS
            mapping_file_opt: '-Xiwyu --mapping_file=$(realpath ./macos.imp)'
      fail-fast: false

    runs-on: ${{ matrix.os }}
    if: ${{ github.repository_owner == 'danmar' }}

    container:
      image: ${{ matrix.image }}

    env:
      QT_VERSION: 6.10.0

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Install missing software on debian/ubuntu
        if: contains(matrix.image, 'debian')
        run: |
          apt-get update
          apt-get install -y cmake clang make libpcre3-dev
          apt-get install -y libgl-dev  # fixes missing dependency for Qt in CMake
          apt-get install -y iwyu

      - name: Install missing software on archlinux
        if: contains(matrix.image, 'archlinux')
        run: |
          set -x
          pacman -Sy
          pacman -S cmake make clang pcre --noconfirm
          pacman -S libglvnd --noconfirm  # fixes missing dependency for Qt in CMake
          pacman-key --init
          pacman-key --recv-key 3056513887B78AEB --keyserver keyserver.ubuntu.com
          pacman-key --lsign-key 3056513887B78AEB 
          pacman -U 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst' 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst' --noconfirm
          echo "[chaotic-aur]" >> /etc/pacman.conf
          echo "Include = /etc/pacman.d/chaotic-mirrorlist" >> /etc/pacman.conf
          pacman -Sy
          pacman -S include-what-you-use --noconfirm
          ln -s iwyu-tool /usr/sbin/iwyu_tool

      - name: Install missing software on Fedora
        if: contains(matrix.image, 'fedora')
        run: |
          dnf install -y cmake clang pcre-devel
          dnf install -y libglvnd-devel  # fixes missing dependency for Qt in CMake
          dnf install -y p7zip-plugins  # required as fallback for py7zr in Qt installation
          dnf install -y python3-pip  # fixes missing pip module in jurplel/install-qt-action
          dnf install -y python3-devel  # fixes building of wheels for jurplel/install-qt-action
          dnf install -y cairo-devel gtk3-devel libcurl-devel lua-devel openssl-devel python3-devel sqlite-devel boost-devel cppunit-devel libsigc++20-devel  # for strict cfg checks
          dnf install -y iwyu
          ln -s iwyu_tool.py /usr/bin/iwyu_tool

      - name: Install missing software on Fedora (libc++)
        if: contains(matrix.image, 'fedora') && matrix.stdlib == 'libc++'
        run: |
          dnf install -y libcxx-devel

      - name: Install missing software on OpenSUSE
        if: contains(matrix.image, 'opensuse')
        run: |
          zypper install -y cmake clang pcre-devel
          zypper install -y include-what-you-use-tools
          ln -s iwyu_tool.py /usr/bin/iwyu_tool

      # coreutils contains "nproc"
      - name: Install missing software on macOS
        if: contains(matrix.os, 'macos')
        run: |
          brew install include-what-you-use pcre coreutils
          # on Apple Silicon files are symlinked under /opt/homebrew/bin
          ln -s /opt/homebrew/bin/iwyu_tool.py /usr/local/bin/iwyu_tool

      # Fails on OpenSUSE:
      # Warning: Failed to restore: Tar failed with error: Unable to locate executable file: tar. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also check the file mode to verify the file is executable.
      # Also the shell is broken afterwards:
      # OCI runtime exec failed: exec failed: unable to start container process: exec: "sh": executable file not found in $PATH: unknown
      #
      # On macos-26 we need to perform the Python setup because the default installation is managed externally managed
      - name: Install Qt ${{ env.QT_VERSION }}
        uses: jurplel/install-qt-action@v4
        with:
          version: ${{ env.QT_VERSION }}
          modules: 'qtcharts'
          setup-python: ${{ contains(matrix.os, 'macos') }}
          install-deps: false
          cache: true

      - name: Generate macOS mappings
        if: contains(matrix.os, 'macos')
        run: |
          set -x

          wget https://raw.githubusercontent.com/include-what-you-use/include-what-you-use/master/mapgen/iwyu-mapgen-apple-libc.py
          python3 iwyu-mapgen-apple-libc.py $(xcrun --show-sdk-path)/usr/include > macos.imp

      - name: Prepare CMake
        run: |
          cmake -S . -B cmake.output -Werror=dev -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DHAVE_RULES=On -DBUILD_TESTING=On -DBUILD_GUI=On -DWITH_QCHART=On -DBUILD_TRIAGE=On -DENABLE_CHECK_INTERNAL=On -DCMAKE_GLOBAL_AUTOGEN_TARGET=On -DDISABLE_DMAKE=On -DCMAKE_DISABLE_PRECOMPILE_HEADERS=On -DCPPCHK_GLIBCXX_DEBUG=Off -DUSE_MATCHCOMPILER=Off -DEXTERNALS_AS_SYSTEM=On -DUSE_LIBCXX=${{ matrix.stdlib == 'libc++' }}
        env:
          CC: clang
          CXX: clang++

      # Fails on Debian:
      # /__w/cppcheck/Qt/6.7.0/gcc_64/libexec/rcc: error while loading shared libraries: libglib-2.0.so.0: cannot open shared object file: No such file or directory
      - name: Prepare CMake dependencies
        run: |
          # make sure the auto-generated GUI sources exist
          make -C cmake.output autogen
          # make sure the precompiled headers exist
          #make -C cmake.output/cli cmake_pch.hxx.pch
          #make -C cmake.output/gui cmake_pch.hxx.pch
          #make -C cmake.output/lib cmake_pch.hxx.pch
          #make -C cmake.output/test cmake_pch.hxx.pch
          # make sure the auto-generated GUI dependencies exist
          make -C cmake.output gui-build-deps
          make -C cmake.output triage-build-ui-deps

      - name: iwyu_tool
        run: |
          # TODO: remove -stdlib= - it should have been taken from the compilation database
          iwyu_tool -p cmake.output -j $(nproc) -- -w -Xiwyu --max_line_length=1024 -Xiwyu --comment_style=long -Xiwyu --quoted_includes_first -Xiwyu --update_comments -stdlib=${{ matrix.stdlib }} ${{ matrix.mapping_file_opt }} ${{ matrix.clang_inc }} > iwyu.log

      # TODO: run with all configurations
      - name: test/cfg
        if: matrix.stdlib == 'libstdc++'
        run: |
          # TODO: redirect to log
          ./test/cfg/runtests.sh
        env:
          IWYU: include-what-you-use
          IWYU_CLANG_INC: ${{ matrix.clang_inc }}

      - uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: Compilation Database (include-what-you-use - ${{ matrix.os }} ${{ matrix.stdlib }})
          path: ./cmake.output/compile_commands.json

      - uses: actions/upload-artifact@v4
        if: ${{ contains(matrix.os, 'macos') && (success() || failure()) }}
        with:
          name: macOS Mappings
          path: |
            ./iwyu-mapgen-apple-libc.py
            ./macos.imp

      - uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: Logs (include-what-you-use - ${{ matrix.os }} ${{ matrix.stdlib }})
          path: ./*.log

  clang-include-cleaner:

    strategy:
      matrix:
        stdlib: [libstdc++, libc++]
        include:
          - stdlib: libstdc++
            use_libcxx: Off
          - stdlib: libc++
            use_libcxx: On
      fail-fast: false

    runs-on: ubuntu-22.04
    if: ${{ github.repository_owner == 'danmar' }}

    env:
      QT_VERSION: 6.10.0

    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Install missing software
        run: |
          sudo apt-get update
          sudo apt-get install -y cmake make libpcre3-dev
          sudo apt-get install -y libgl-dev  # missing dependency for using Qt in CMake

      - name: Install clang
        run: |
          sudo apt-get purge --auto-remove llvm python3-lldb-14 llvm-14
          wget https://apt.llvm.org/llvm.sh
          chmod +x llvm.sh
          sudo ./llvm.sh 22
          sudo apt-get install -y clang-tools-22

      - name: Install libc++
        if: matrix.stdlib == 'libc++'
        run: |
          sudo apt-get install -y libc++-22-dev

      - name: Install Qt ${{ env.QT_VERSION }}
        uses: jurplel/install-qt-action@v4
        with:
          version: ${{ env.QT_VERSION }}
          modules: 'qtcharts'
          setup-python: 'false'
          install-deps: false
          cache: true

      - name: Prepare CMake
        run: |
          cmake -S . -B cmake.output -Werror=dev -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DHAVE_RULES=On -DBUILD_TESTING=On -DBUILD_GUI=On -DWITH_QCHART=On -DBUILD_TRIAGE=On -DENABLE_CHECK_INTERNAL=On -DCMAKE_GLOBAL_AUTOGEN_TARGET=On -DDISABLE_DMAKE=On -DCMAKE_DISABLE_PRECOMPILE_HEADERS=On -DCPPCHK_GLIBCXX_DEBUG=Off -DUSE_MATCHCOMPILER=Off -DEXTERNALS_AS_SYSTEM=On -DUSE_LIBCXX=${{ matrix.use_libcxx }}
        env:
          CC: clang-22
          CXX: clang++-22

      - name: Prepare CMake dependencies
        run: |
          # make sure the auto-generated GUI sources exist
          make -C cmake.output autogen
          # make sure the precompiled headers exist
          #make -C cmake.output/cli cmake_pch.hxx.pch
          #make -C cmake.output/gui cmake_pch.hxx.pch
          #make -C cmake.output/lib cmake_pch.hxx.pch
          #make -C cmake.output/test cmake_pch.hxx.pch
          # make sure the auto-generated GUI dependencies exist
          make -C cmake.output gui-build-deps

      - name: clang-include-cleaner
        run: |
          # TODO: run multi-threaded
          find $PWD/cli $PWD/lib $PWD/test $PWD/gui -maxdepth 1 -name "*.cpp" | xargs -t -n 1 clang-include-cleaner-22 --print=changes --extra-arg=-w --extra-arg=-stdlib=${{ matrix.stdlib }} -p cmake.output > clang-include-cleaner.log 2>&1

      - uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: Compilation Database (clang-include-cleaner - ${{ matrix.stdlib }})
          path: ./cmake.output/compile_commands.json

      - uses: actions/upload-artifact@v4
        if: success() || failure()
        with:
          name: Logs (clang-include-cleaner - ${{ matrix.stdlib }})
          path: ./*.log
