name: android
on:
  push:
    branches: [master]
    paths:
    - '.github/workflows/android.yml'
    - 'CMakeLists.txt'
    - 'cmake/**'
    - 'src/*'
    - 'src/layer/*'
    - 'src/layer/arm/**'
    - 'src/layer/riscv/**'
    - 'src/layer/x86/**'
    - 'src/layer/vulkan/**'
  pull_request:
    branches: [master]
    paths:
    - '.github/workflows/android.yml'
    - 'CMakeLists.txt'
    - 'cmake/**'
    - 'src/*'
    - 'src/layer/*'
    - 'src/layer/arm/**'
    - 'src/layer/riscv/**'
    - 'src/layer/x86/**'
    - 'src/layer/vulkan/**'
concurrency:
  group: android-${{ github.ref }}
  cancel-in-progress: true
permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      NCNN_CMAKE_OPTIONS: |
        -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_LATEST_HOME/build/cmake/android.toolchain.cmake \
        -DANDROID_PLATFORM=android-21 \
        -DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON \
        -DCMAKE_INSTALL_PREFIX=install \
        -DCMAKE_BUILD_TYPE=Release \
        -DNCNN_VULKAN=ON \

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - name: armeabi-v7a
      run: |
        mkdir build-armeabi-v7a && cd build-armeabi-v7a
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="armeabi-v7a" -DANDROID_ARM_NEON=ON ..
        cmake --build . -j $(nproc)
    - name: arm64-v8a
      run: |
        mkdir build-arm64-v8a && cd build-arm64-v8a
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="arm64-v8a" ..
        cmake --build . -j $(nproc)
    - name: x86
      run: |
        mkdir build-x86 && cd build-x86
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="x86" ..
        cmake --build . -j $(nproc)
    - name: x86_64
      run: |
        mkdir build-x86_64 && cd build-x86_64
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="x86_64" ..
        cmake --build . -j $(nproc)
    - name: riscv64
      run: |
        mkdir build-riscv64 && cd build-riscv64
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="riscv64" ..
        cmake --build . -j $(nproc)

    - name: armeabi-v7a-shared
      run: |
        mkdir build-armeabi-v7a-shared && cd build-armeabi-v7a-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="armeabi-v7a" -DANDROID_ARM_NEON=ON -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
    - name: arm64-v8a-shared
      run: |
        mkdir build-arm64-v8a-shared && cd build-arm64-v8a-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="arm64-v8a" -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
    - name: x86-shared
      run: |
        mkdir build-x86-shared && cd build-x86-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="x86" -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
    - name: x86_64-shared
      run: |
        mkdir build-x86_64-shared && cd build-x86_64-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="x86_64" -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
    - name: riscv64-shared
      run: |
        mkdir build-riscv64-shared && cd build-riscv64-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="riscv64" -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)

  ndk-r16b:
    runs-on: ubuntu-latest
    env:
      NCNN_CMAKE_OPTIONS: |
        -DCMAKE_TOOLCHAIN_FILE=$GITHUB_WORKSPACE/android-ndk-r16b/build/cmake/android.toolchain.cmake \
        -DANDROID_PLATFORM=android-21 \
        -DCMAKE_INSTALL_PREFIX=install \
        -DCMAKE_BUILD_TYPE=Release \
        -DNCNN_VULKAN=ON \

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true

    - name: ndk-r16b
      env:
        DEBIAN_FRONTEND: noninteractive
      run: |
        pushd /usr/lib/x86_64-linux-gnu/
        sudo ln -s libncurses.so.6 libncurses.so.5
        sudo ln -s libtinfo.so.6 libtinfo.so.5
        popd
        wget -q https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip -O $GITHUB_WORKSPACE/android-ndk-r16b-linux-x86_64.zip
        cd $GITHUB_WORKSPACE && unzip -q android-ndk-r16b-linux-x86_64.zip

    - name: armeabi-v7a
      run: |
        mkdir build-armeabi-v7a && cd build-armeabi-v7a
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="armeabi-v7a" -DANDROID_ARM_NEON=ON ..
        cmake --build . -j $(nproc)
    - name: armeabi-v7a-no-neon
      run: |
        mkdir build-armeabi-v7a-no-neon && cd build-armeabi-v7a-no-neon
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="armeabi-v7a" -DANDROID_ARM_NEON=OFF ..
        cmake --build . -j $(nproc)
    - name: arm64-v8a
      run: |
        mkdir build-arm64-v8a && cd build-arm64-v8a
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="arm64-v8a" ..
        cmake --build . -j $(nproc)

    - name: armeabi-v7a-shared
      run: |
        mkdir build-armeabi-v7a-shared && cd build-armeabi-v7a-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="armeabi-v7a" -DANDROID_ARM_NEON=ON -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
    - name: armeabi-v7a-no-neon-shared
      run: |
        mkdir build-armeabi-v7a-no-neon-shared && cd build-armeabi-v7a-no-neon-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="armeabi-v7a" -DANDROID_ARM_NEON=OFF -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
    - name: arm64-v8a-shared
      run: |
        mkdir build-arm64-v8a-shared && cd build-arm64-v8a-shared
        cmake ${{ env.NCNN_CMAKE_OPTIONS }} -DANDROID_ABI="arm64-v8a" -DNCNN_SHARED_LIB=ON ..
        cmake --build . -j $(nproc)
