name: backend-coreml

on:
  push:
    paths:
      - "csrc/**"
      - "demo/csrc/**"
      - "CMakeLists.txt"

  pull_request:
    paths:
      - "csrc/**"
      - "demo/csrc/**"
      - "CMakeLists.txt"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
env:
  DEVELOPER_DIR: /Applications/Xcode_13.4.1.app/Contents/Developer
permissions:
  contents: read

jobs:
  build_macos_arm64:
    runs-on: macos-12
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: 'recursive'
      - name: install opencv
        run: |
          wget https://github.com/irexyc/mmdeploy-ci-resource/releases/download/opencv/opencv-osx-arm64-4.6.0.tar.gz
          mkdir $GITHUB_WORKSPACE/opencv-install
          tar xf opencv-osx-arm64-4.6.0.tar.gz -C $GITHUB_WORKSPACE/opencv-install
      - name: install libtorch
        run: |
          wget https://github.com/irexyc/mmdeploy-ci-resource/releases/download/libtorch/libtorch-osx-arm64-1.8.0.tar.gz
          mkdir $GITHUB_WORKSPACE/libtorch-install
          tar xf libtorch-osx-arm64-1.8.0.tar.gz -C $GITHUB_WORKSPACE/libtorch-install
      - name: build-static-lib
        run: |
          mkdir build && cd build
          cmake .. -DCMAKE_OSX_ARCHITECTURES="arm64" \
            -DCMAKE_SYSTEM_PROCESSOR="arm64" \
            -DMMDEPLOY_BUILD_SDK=ON \
            -DOpenCV_DIR=$GITHUB_WORKSPACE/opencv-install/lib/cmake/opencv4 \
            -DTorch_DIR=$GITHUB_WORKSPACE/libtorch-install/share/cmake/Torch \
            -DMMDEPLOY_TARGET_BACKENDS="coreml" \
            -DMMDEPLOY_BUILD_EXAMPLES=ON \
            -DMMDEPLOY_BUILD_SDK_MONOLITHIC=OFF \
            -DMMDEPLOY_SHARED_LIBS=OFF
          cmake --build . -j 3
          cmake --build . --target install
      - name: build-monolithic-lib
        run: |
          mkdir build-shared && cd build-shared
          cmake .. -DCMAKE_OSX_ARCHITECTURES="arm64" \
            -DCMAKE_SYSTEM_PROCESSOR="arm64" \
            -DMMDEPLOY_BUILD_SDK=ON \
            -DMMDEPLOY_TARGET_DEVICES="cpu" \
            -DMMDEPLOY_CODEBASES=all \
            -DOpenCV_DIR=$GITHUB_WORKSPACE/opencv-install/lib/cmake/opencv4 \
            -DTorch_DIR=$GITHUB_WORKSPACE/libtorch-install/share/cmake/Torch \
            -DMMDEPLOY_TARGET_BACKENDS="coreml" \
            -DMMDEPLOY_BUILD_SDK_MONOLITHIC=ON \
            -DMMDEPLOY_SHARED_LIBS=OFF \
            -DMMDEPLOY_BUILD_EXAMPLES=ON
          cmake --build . -j 3
          cmake --build . --target install
