name: CI (clang)

on:
  workflow_dispatch: # allows manual triggering
  push:
    branches:
      - master
    paths: ['.github/workflows/build-clang.yml',
            '**/CMakeLists.txt',
            '**/Makefile',
            '**/*.mk',
            '**/*.cmake',
            '**/*.in',
            '**/*.h',
            '**/*.hpp',
            '**/*.c',
            '**/*.cpp',
            '**/*.cu',
            '**/*.cuh',
            '**/*.cl']

  pull_request:
    types: [opened, synchronize, reopened]
    paths-ignore:
      - 'bindings/ruby/**'        # handled by bindings-ruby.yml
      - 'bindings/go/**'          # handled by bindings-go.yml
      - 'examples/addon.node/**'  # handled by examples.yml

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

env:
  ubuntu_image: "ubuntu:22.04"

jobs:
  ubuntu-22-clang:
    runs-on: ubuntu-22.04

    strategy:
      fail-fast: false
      matrix:
        build: [Debug, Release]
        #arch: [linux/amd64, linux/arm64, linux/arm/v7, linux/ppc64le]
        # TODO: arm/v7 disabled due to clang bug
        #       https://github.com/ggerganov/whisper.cpp/actions/runs/9657764109/job/26637633042?pr=2256#step:4:1990
        arch: [linux/amd64, linux/ppc64le]

    steps:
      - name: Clone
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6

      - name: Set CCACHE_DIR
        run: echo "CCACHE_DIR=${{ runner.temp }}/ccache" >> $GITHUB_ENV

      - name: ccache
        uses: ggml-org/ccache-action@v1.2.21
        with:
          key: clang-${{ matrix.arch }}-${{ matrix.build }}
          evict-old-files: 1d
          save: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4

      - name: Build ${{ matrix.arch }}
        run: |
          docker run --platform ${{ matrix.arch }} --rm \
            -v ${{ github.workspace }}:/workspace \
            -v ${CCACHE_DIR}:${CCACHE_DIR} \
            -e CCACHE_DIR=${CCACHE_DIR} \
            -w /workspace ${{ env.ubuntu_image }} /bin/sh -c '
            set -e
            export DEBIAN_FRONTEND=noninteractive
            sed -i "s|archive.ubuntu.com|mirrors.kernel.org|g" /etc/apt/sources.list
            sed -i "s|security.ubuntu.com|mirrors.kernel.org|g" /etc/apt/sources.list

            apt update
            apt install -y clang build-essential cmake libsdl2-dev git ccache
            cmake . -DWHISPER_SDL2=ON -DCMAKE_BUILD_TYPE=${{ matrix.build }} \
              -DCMAKE_CXX_COMPILER=clang++ \
              -DCMAKE_C_COMPILER=clang \
              -DCMAKE_C_COMPILER_LAUNCHER=ccache \
              -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
            make
            ctest -L gh --output-on-failure'

  ubuntu-22-clang-arm64:
    runs-on: ubuntu-22.04-arm

    strategy:
      fail-fast: false
      matrix:
        build: [Debug, Release]

    steps:
      - name: Clone
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6

      - name: ccache
        uses: ggml-org/ccache-action@v1.2.21
        with:
          key: clang-arm64-${{ matrix.build }}
          evict-old-files: 1d
          save: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y clang build-essential cmake libsdl2-dev git

      - name: Build and Test
        run: |
          cmake . -DWHISPER_SDL2=ON \
            -DCMAKE_BUILD_TYPE=${{ matrix.build }} \
            -DCMAKE_CXX_COMPILER=clang++ \
            -DCMAKE_C_COMPILER=clang \
            -DGGML_NATIVE=OFF \
            -DGGML_CPU_ARM_ARCH=armv8-a
          make
          ctest -L gh --output-on-failure
