name: IWYU checker

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - master
    paths:
      - ".github/workflows/IWYU_scan.yml"
      - "include/**"
      - "lib/**"
      - "plugins/**"
      - "test/**"
      - "thirdparty/**"
      - "tools/**"
      - "CMakeLists.txt"
  pull_request:
    branches:
      - master
      - 'proposal/**'
    paths:
      - ".github/workflows/IWYU_scan.yml"
      - "include/**"
      - "lib/**"
      - "plugins/**"
      - "test/**"
      - "thirdparty/**"
      - "tools/**"
      - "CMakeLists.txt"

jobs:
  get_version:
    name: Retrieve version information
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.prep.outputs.version }}
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Ensure git safe directory
      run: |
        git config --global --add safe.directory $(pwd)
    - name: Get version
      id: prep
      run: |
        # Retrieve annotated tags. Details: https://github.com/actions/checkout/issues/290
        git fetch --tags --force
        git config --global --add safe.directory $(pwd)
        echo "version=$(git describe --match '[0-9].[0-9]*' --tag)" >> $GITHUB_OUTPUT

  build_fedora:
    name: Fedora latest
    needs: get_version
    runs-on: ubuntu-latest
    container:
      image: fedora:latest

    steps:
    - name: Install requirements
      run: |
        dnf update -y
        dnf install -y cmake ninja-build llvm llvm-devel lld-devel clang git file rpm-build dpkg-dev clang-devel spdlog-devel
        curl -L -O https://github.com/include-what-you-use/include-what-you-use/archive/refs/tags/0.20.zip
        unzip 0.20.zip
        mkdir build && cd build
        cmake -G "Unix Makefiles" ../include-what-you-use-0.20
        make -j
        make install
        cd ..

    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Ensure git safe directory
      run: |
        git config --global --add safe.directory $(pwd)

    - name: Build and scan WasmEdge with IWYU
      run: |
        cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Debug -DWASMEDGE_BUILD_TESTS=ON -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=include-what-you-use .
        cmake --build build > iwyu_fedora.log

    - uses: actions/upload-artifact@v3
      with:
        name: iwyu_fedora.log
        path: iwyu_fedora.log


  build_macos:
    name: macOS
    runs-on: macos-latest
    needs: get_version

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Ensure git safe directory
      run: |
        git config --global --add safe.directory $(pwd)

    - name: Build and scan WasmEdge with IWYU
      shell: bash
      run: |
        brew install llvm ninja cmake
        export LLVM_DIR="/usr/local/opt/llvm/lib/cmake"
        export Clang_DIR="/usr/local/opt/llvm/lib/cmake/clang"
        export CC=clang
        export CXX=clang++

        cd ../../
        curl -L -O https://github.com/include-what-you-use/include-what-you-use/archive/refs/tags/0.20.zip
        unzip 0.20.zip
        mkdir build && cd build
        cmake -G "Unix Makefiles" ../include-what-you-use-0.20
        make -j install
        cd ../WasmEdge/WasmEdge

        cmake -Bbuild -GNinja -DWASMEDGE_BUILD_TESTS=ON -DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=include-what-you-use -DWASMEDGE_BUILD_PACKAGE="TGZ" .
        cmake --build build > iwyu_macos.log

    - uses: actions/upload-artifact@v3
      with:
        name: iwyu_macOS.log
        path: iwyu_macOS.log

