name: CI (android)

on:
  workflow_dispatch:
  push:
    branches:
      - master
    paths:
      - '.github/workflows/build-android.yml'
      - '**/CMakeLists.txt'
      - '**/.cmake'
      - '**/*.h'
      - '**/*.hpp'
      - '**/*.c'
      - '**/*.cpp'

  pull_request:
    types: [opened, synchronize, reopened]
    paths:
      - '.github/workflows/build-android.yml'
      - 'examples/llama.android/**'

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

env:
  GGML_NLOOP: 3
  GGML_N_THREADS: 1
  LLAMA_ARG_LOG_COLORS: 1
  LLAMA_ARG_LOG_PREFIX: 1
  LLAMA_ARG_LOG_TIMESTAMPS: 1

jobs:
  default:
    runs-on: ubuntu-latest

    steps:
      - name: Clone
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          lfs: false

      - name: Set up JDK
        uses: actions/setup-java@v5
        with:
          java-version: 17
          distribution: zulu

      - name: Setup Android SDK
        uses: android-actions/setup-android@40fd30fb8d7440372e1316f5d1809ec01dcd3699 # v4.0.1
        with:
          log-accepted-android-sdk-licenses: false

      - name: Build
        run: |
          cd examples/llama.android
          ./gradlew build --no-daemon

  ndk:
    runs-on: ubuntu-latest
    container:
      image: 'ghcr.io/snapdragon-toolchain/arm64-android:v0.3'
    defaults:
      run:
        shell: bash

    steps:
      - name: Clone
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          lfs: false

      - name: Dependencies
        run: |
          apt-get update
          apt-get install -y build-essential

      - name: Build
        id: ndk_build
        run: |
          cmake -D ANDROID_ABI=arm64-v8a -D ANDROID_PLATFORM=android-31 -D CMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake -D GGML_NATIVE=OFF -DGGML_CPU_ARM_ARCH=armv8.5-a+fp16+i8mm -G Ninja -D LLAMA_OPENSSL=OFF -D GGML_OPENMP=OFF -B build
          cmake --build build
          cmake --install build --prefix pkg-adb/llama.cpp

      - name: Upload Android Build Artifact
        if: ${{ always() && steps.ndk_build.outcome == 'success' }}
        uses: actions/upload-artifact@v6
        with:
          name: llama-cpp-android-arm64-cpu
          path: pkg-adb/llama.cpp

  arm64:
    runs-on: ubuntu-latest

    env:
      NDK_VERSION: "29.0.14206865"

    steps:
      - name: Clone
        id: checkout
        uses: actions/checkout@v6

      # note : disabled to spare some cache space (https://github.com/ggml-org/llama.cpp/pull/23789)
      #        for some reason, the ccache does not improve the build time in this case
      # example:
      #   cache off: https://github.com/ggerganov/tmp2/actions/runs/26534713799/job/78160400831
      #   cache on:  https://github.com/ggerganov/tmp2/actions/runs/26534713799/job/78224189394
      #
      #- name: ccache
      #  uses: ggml-org/ccache-action@v1.2.21
      #  with:
      #    key: android-ubuntu-arm64
      #    evict-old-files: 1d
      #    save: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}

      - name: Set up JDK
        uses: actions/setup-java@v5
        with:
          java-version: 17
          distribution: temurin

      - name: Setup Android SDK
        uses: android-actions/setup-android@40fd30fb8d7440372e1316f5d1809ec01dcd3699 # v4.0.1
        with:
          log-accepted-android-sdk-licenses: false

      - name: Install NDK
        run: |
          sdkmanager "ndk;${{ env.NDK_VERSION }}"
          echo "ANDROID_NDK=${ANDROID_SDK_ROOT}/ndk/${{ env.NDK_VERSION }}" >> $GITHUB_ENV

      - name: Build
        id: cmake_build
        run: |
          cmake -B build \
            -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
            -DANDROID_ABI=arm64-v8a \
            -DANDROID_PLATFORM=android-28 \
            -DLLAMA_FATAL_WARNINGS=ON \
            -DGGML_BACKEND_DL=ON \
            -DGGML_NATIVE=OFF \
            -DGGML_CPU_ALL_VARIANTS=ON \
            -DGGML_OPENMP=OFF \
            -DLLAMA_BUILD_BORINGSSL=ON \
            -DGGML_RPC=ON
          time cmake --build build --config Release -j $(nproc)
