name: Core Cache

on:
  push:
    branches: [main]
    paths:
      - 'jni/deltachat-core-rust'
      - 'jni/Android.mk'
      - 'jni/Application.mk'
      - 'jni/dc_wrapper.c'
      - 'scripts/ndk-make.sh'

permissions: {}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: recursive
          persist-credentials: false

      - uses: android-actions/setup-android@40fd30fb8d7440372e1316f5d1809ec01dcd3699 # v4.0.1
      - uses: nttld/setup-ndk@ed92fe6cadad69be94a966a7ee3271275e62f779 # v1.6.0
        id: setup-ndk
        with:
          ndk-version: "r29"

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          workspaces: jni/deltachat-core-rust

      - name: Get deltachat-core-rust submodule hash
        id: core-hash
        run: echo "hash=$(git rev-parse HEAD:jni/deltachat-core-rust)" >> $GITHUB_OUTPUT

      - name: Cache compiled core
        id: cache-core
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        with:
          path: |
            jni/arm64-v8a
            libs/arm64-v8a
          key: core-arm64-v8a-${{ steps.core-hash.outputs.hash }}-${{ hashFiles('jni/Android.mk', 'jni/Application.mk', 'jni/dc_wrapper.c', 'scripts/ndk-make.sh') }}-ndk-r27

      - name: Compile core
        if: steps.cache-core.outputs.cache-hit != 'true'
        env:
          ANDROID_NDK_ROOT: ${{ steps.setup-ndk.outputs.ndk-path }}
        run: |
          export PATH="${PATH}:${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/bin/"
          scripts/install-toolchains.sh && scripts/ndk-make.sh arm64-v8a
