name: Android ARM64 with vcpkg
on:
  workflow_dispatch:
  pull_request:
  merge_group:
  push:
    branches:
      # - master
      - 'releases/**'

concurrency:
  # github.ref is not unique in post-commit
  group: ${{ github.event_name == 'push' && github.run_id || github.ref }}-android-arm64-vcpkg
  cancel-in-progress: true

permissions: read-all

jobs:
  Smart_CI:
    runs-on: ubuntu-latest
    outputs:
      affected_components: "${{ steps.smart_ci.outputs.affected_components }}"
      changed_components: "${{ steps.smart_ci.outputs.changed_components }}"
      skip_workflow: "${{ steps.smart_ci.outputs.skip_workflow }}"
    steps:
      - name: checkout action
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        timeout-minutes: 15
        with:
          sparse-checkout: .github/actions/smart-ci

      - name: Get affected components
        id: smart_ci
        uses: ./.github/actions/smart-ci
        with:
          repository: ${{ github.repository }}
          pr: ${{ github.event.number }}
          commit_sha: ${{ github.sha }}
          ref_name: ${{ github.ref_name }}
          component_pattern: "category: (.*)"
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          skip_when_only_listed_labels_set: 'docs'
          skip_when_only_listed_files_changed: '*.md,*.rst,*.png,*.jpg,*.svg,*/layer_tests_summary/*,*/conformance/*'

  Docker:
    needs: Smart_CI
    if: "!needs.smart_ci.outputs.skip_workflow"
    runs-on: aks-linux-4-cores-16gb-docker-build
    container:
      image: openvinogithubactions.azurecr.io/docker_build:0.2
      volumes:
        - /mount:/mount
    outputs:
      images: "${{ steps.handle_docker.outputs.images }}"
    steps:
      - name: Checkout
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        timeout-minutes: 15

      - uses: ./.github/actions/handle_docker
        id: handle_docker
        with:
          images: |
            ov_build/ubuntu_22_04_android
          registry: 'openvinogithubactions.azurecr.io'
          dockerfiles_root_dir: '.github/dockerfiles'
          changed_components: ${{ needs.smart_ci.outputs.changed_components }}

  Build:
    needs: [Smart_CI, Docker]
    timeout-minutes: 150
    defaults:
      run:
        shell: bash
    runs-on: aks-linux-16-cores-32gb
    container:
      image: ${{ fromJSON(needs.docker.outputs.images).ov_build.ubuntu_22_04_android }}
      volumes:
        - /mount:/mount
      options: -e SCCACHE_AZURE_BLOB_CONTAINER -e SCCACHE_AZURE_CONNECTION_STRING
    env:
      DEBIAN_FRONTEND: noninteractive # to prevent apt-get from waiting user input
      CMAKE_GENERATOR: 'Ninja'
      CMAKE_CXX_COMPILER_LAUNCHER: sccache
      CMAKE_C_COMPILER_LAUNCHER: sccache
      SCCACHE_IGNORE_SERVER_IO_ERROR: 1
      SCCACHE_SERVER_PORT: 35555
      SCCACHE_ERROR_LOG: /__w/openvino/sccache_log.txt
      SCCACHE_LOG: warn
      OPENVINO_REPO: '/__w/openvino/openvino/openvino'
      VCPKG_ROOT: '/__w/openvino/openvino/vcpkg'
      BUILD_DIR: '/__w/openvino/openvino/build'
      ANDROID_TOOLS: '/deps/android_tools'
      ANDROID_NDK_HOME: '/deps/android_tools/ndk-bundle'
      ANDROID_SDK_VERSION: 29
      ANDROID_ABI_CONFIG: arm64-v8a
      VCPKG_TARGET_TRIPLET: arm64-android
      VCPKG_DEFAULT_BINARY_CACHE: '/mount/caches/ccache/android_arm64/vcpkg_cache'
      VCPKG_FORCE_SYSTEM_BINARIES: '1'
      SCCACHE_AZURE_KEY_PREFIX: android_arm64
    steps:
      - name: Clone OpenVINO
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        timeout-minutes: 15
        with:
          path: 'openvino'

      - name: Get VCPKG version and put it into GitHub ENV
        run: echo VCPKG_VERSION=$(cat ${OPENVINO_REPO}/vcpkg.json | python3 -c "import sys, json; print(json.load(sys.stdin)['builtin-baseline'])") >> $GITHUB_ENV

      - name: Init submodules for non vcpkg dependencies
        run: |
          pushd ${OPENVINO_REPO}
            git submodule update --init -- ${OPENVINO_REPO}/src/plugins
            git submodule update --init -- ${OPENVINO_REPO}/thirdparty/zlib
            git submodule update --init -- ${OPENVINO_REPO}/thirdparty/json
            git submodule update --init -- ${OPENVINO_REPO}/thirdparty/gtest
            git submodule update --init -- ${OPENVINO_REPO}/thirdparty/gflags
          popd

      - name: Clone vcpkg
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        timeout-minutes: 15
        with:
          repository: 'microsoft/vcpkg'
          ref: ${{ env.VCPKG_VERSION }}
          path: 'vcpkg'
          fetch-depth: '0'

      #
      # Print system info
      #

      - name: System info
        uses: ./openvino/.github/actions/system_info

      #
      # Build
      #

      - name: Build vcpkg
        run: |
          mkdir -p ${VCPKG_DEFAULT_BINARY_CACHE}
          ${VCPKG_ROOT}/bootstrap-vcpkg.sh
          # patch vcpkg default toolchain to build only Release configuration
          echo "set(VCPKG_BUILD_TYPE release)" >> ${VCPKG_ROOT}/triplets/arm64-android.cmake

      - name: CMake - configure
        run: |
          cmake \
            -G '${{ env.CMAKE_GENERATOR }}' \
            -DENABLE_INTEL_GPU=ON \
            -DENABLE_TESTS=ON \
            -DENABLE_SYSTEM_OPENCL=ON \
            -DENABLE_SYSTEM_PROTOBUF=ON \
            -DENABLE_SYSTEM_PUGIXML=ON \
            -DENABLE_SYSTEM_SNAPPY=ON \
            -DENABLE_SYSTEM_TBB=ON \
            -DENABLE_SYSTEM_FLATBUFFERS=ON \
            -DANDROID_ABI=${{ env.ANDROID_ABI_CONFIG }} \
            -DANDROID_PLATFORM=${{ env.ANDROID_SDK_VERSION }} \
            -DVCPKG_TARGET_TRIPLET=${{ env.VCPKG_TARGET_TRIPLET }} \
            -DVCPKG_HOST_TRIPLET=x64-linux-release \
            -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake \
            -DVCPKG_CHAINLOAD_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
            -DCMAKE_COMPILE_WARNING_AS_ERROR=ON \
            -DCMAKE_CXX_COMPILER_LAUNCHER=${{ env.CMAKE_CXX_COMPILER_LAUNCHER }} \
            -DCMAKE_C_COMPILER_LAUNCHER=${{ env.CMAKE_C_COMPILER_LAUNCHER }} \
            -S ${OPENVINO_REPO} \
            -B ${BUILD_DIR}

      - name: Clean ccache stats
        run: ${SCCACHE_PATH} --zero-stats

      - name: Cmake - build
        run: cmake --build ${BUILD_DIR} --parallel $(nproc)

      - name: Show ccache stats
        run: ${SCCACHE_PATH} --show-stats

      #
      # Upload build logs
      #
      - name: Upload build logs
        uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
        if: always()
        with:
          name: build_logs
          path: ${{ env.SCCACHE_ERROR_LOG }}
          if-no-files-found: 'ignore'

  Overall_Status:
    name: ci/gha_overall_status_android_arm64
    needs: [Smart_CI, Build]
    if: ${{ always() }}
    runs-on: ubuntu-latest
    steps:
      - name: Check status of all jobs
        if: >-
          ${{
            contains(needs.*.result, 'failure') ||
            contains(needs.*.result, 'cancelled')
          }}
        run: exit 1