# *******************************************************************************
# Copyright 2025 Intel Corporation
# Copyright 2025 Arm Limited and affiliates.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# *******************************************************************************

name: "Clang-Tidy"

on:
  pull_request:
    types: [opened, edited, synchronize, reopened]
    paths:
      - ".github/automation/aarch64/**"
      - ".github/automation/x64/**"
      - ".github/workflows/clang-tidy.yml"
      - "cmake/**"
      - "examples/**"
      - "include/**"
      - "src/common/**"
      - "src/cpu/*"
      - "src/cpu/aarch64/**"
      - "src/cpu/gemm/**"
      - "src/cpu/matmul/**"
      - "src/cpu/reorder/**"
      - "src/cpu/rnn/**"
      - "src/cpu/x64/**"
      - "src/gpu/*"
      - "src/gpu/intel/**"
      - "src/graph/**"
      - "tests/**"
      - "CMakeLists.txt"

## Declare default permissions as read only.
permissions: read-all

# Kill stale checks
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  pr-clang-tidy:
    strategy:
      fail-fast: false
      matrix:
        config: [
          { arch: AArch64, label: ubuntu-24.04-arm},
          { arch: x64, label: ubuntu-latest},
        ]

    name: Clang-Tidy - ${{matrix.config.arch}}
    runs-on: ${{matrix.config.label}}
    steps:
      - name: Checkout oneDNN
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          path: oneDNN
          fetch-depth: 0

      - name: Install clang
        run: |
          sudo apt-get update
          sudo apt-get install -y clang libomp-dev ocl-icd-libopencl1 ocl-icd-opencl-dev clang-tidy

      # This needs to run on all platforms even if it is not used.
      - name: Read ci.json versions file
        id: get-versions
        run: |
          content=`cat ${{ github.workspace }}/oneDNN/.github/automation/aarch64/ci.json`
          content="${content//[$'\t\r\n$ ']}"
          echo "output=$content" >> $GITHUB_OUTPUT

      - if: ${{ matrix.config.arch == 'AArch64' }}
        name: Clone ACL
        run: ${{ github.workspace }}/oneDNN/.github/automation/aarch64/build_acl.sh
        env:
          ACL_ACTION: clone
          ACL_ROOT_DIR: ${{ github.workspace }}/ComputeLibrary
          ACL_VERSION: ${{ fromJson(steps.get-versions.outputs.output).dependencies.acl }}

      # We can avoid having to actually build ComputeLibrary by faking the files
      # that cmake/FindACL.cmake and cmake/ACL.cmake expect to see.
      - if: ${{ matrix.config.arch == 'AArch64' }}
        name: Fake ACL Build
        working-directory: ${{ github.workspace }}/ComputeLibrary/
        env:
          ACL_VERSION: ${{ fromJson(steps.get-versions.outputs.output).dependencies.acl }}
        run: |
          mkdir ./build
          echo "arm_compute_version=${ACL_VERSION}" > ./build/arm_compute_version.embed
          touch ./build/libarm_compute.so
          touch ./build/libarm_compute_graph.so

      - if: ${{ matrix.config.arch == 'x64' }}
        name: Configure oneDNN - x64
        working-directory: ${{github.workspace}}/oneDNN
        run: .github/automation/x64/build_linters.sh
        env:
          ONEDNN_ACTION: configure

      - if: ${{ matrix.config.arch == 'AArch64' }}
        name: Configure oneDNN - AArch64
        working-directory: ${{github.workspace}}/oneDNN
        run: .github/automation/aarch64/build.sh
        env:
          BUILD_TOOLSET: clang
          ACL_ROOT_DIR: ${{github.workspace}}/ComputeLibrary
          ONEDNN_ACTION: configure

      - name: Check source files
        working-directory: ${{github.workspace}}/oneDNN
        shell: bash
        run: |
          set +e
          echo -e "Checking Clang-Tidy $(clang-tidy --version)\n"
          touch clang-tidy.log
          analysis_list="$(git diff --name-only ${{ github.event.pull_request.base.sha }} | grep -E '\.c(pp|xx)?$')"
          if [[ -z "$analysis_list" ]]; then
            echo "No source files to check."
            exit 0
          else
            printf "File list:\n$analysis_list\n"
            run-clang-tidy -j $(nproc) -format -p ./build $analysis_list 2>&1 | tee -a clang-tidy.log
          fi
          grep -i -E "warning:|error:" clang-tidy.log | sort -u
          grep -q -i -E "warning:|error:" clang-tidy.log && exit 1 || true
