# @generated DO NOT EDIT MANUALLY
# Template is at:    .github/templates/ios_ci_workflow.yml.j2
# Generation script: .github/scripts/generate_ci_workflows.py
name: ios-12-5-1-arm64-metal

on:
  push:
    branches:
      - master
      - release/*
    tags:
      - 'ciflow/all/*'
      - 'ciflow/ios/*'
      - 'ciflow/macos/*'
      - 'ciflow/trunk/*'
  workflow_dispatch:

env:
  BUILD_ENVIRONMENT: ios-12-5-1-arm64-metal
  IN_CI: 1
  IS_GHA: 1
  IOS_PLATFORM: OS
  IOS_ARCH: arm64


jobs:

  build:
    # NOTE: These builds will not run successfully without running on `pytorch/pytorch` due to the limitations
    #       of accessing secrets from forked pull requests and IOS' dependency on secrets for their build/test
    if: ${{ github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository }}
    runs-on: macos-10.15
    timeout-minutes: 240
    env:
      JOB_BASE_NAME: ios-12-5-1-arm64-metal-build
      IOS_CERT_KEY_2022: ${{ secrets.IOS_CERT_KEY_2022 }}
      IOS_CERT_SECRET: ${{ secrets.IOS_CERT_SECRET }}
      IOS_DEV_TEAM_ID: ${{ secrets.IOS_DEV_TEAM_ID }}
      IOS_SIGN_KEY_2022: ${{ secrets.IOS_SIGN_KEY_2022 }}
      PR_LABELS: ${{ toJson(github.event.pull_request.labels.*.name) }}
    steps:
      - name: print labels
        run: echo "${PR_LABELS}"
      - name: Checkout PyTorch
        uses: zhouzhuojie/checkout@05b13c9a0d21f08f6d5e64a1d5042246d13619d9
        with:
          # deep clone, to allow use of git merge-base
          fetch-depth: 0
          submodules: recursive
      - name: Clean PyTorch checkout
        run: |
          # Remove any artifacts from the previous checkouts
          git clean -fxd
      - name: Populate CI build options
        run: |
          # Most builds use the lite interpreter, if certain builds shouldn't
          # build the lite interpreter this env variable should get over-written
          # in the following case statement
          echo "BUILD_LITE_INTERPRETER=1" >> "${GITHUB_ENV}"

          case ${BUILD_ENVIRONMENT} in
            *metal*)
              echo "USE_PYTORCH_METAL=1" >> "${GITHUB_ENV}"
              ;;
            *full_jit*)
              echo "BUILD_LITE_INTERPRETER=0" >> "${GITHUB_ENV}"
              ;;
            *custom*)
              echo "SELECTED_OP_LIST=${GITHUB_WORKSPACE}/ios/TestApp/custom_build/mobilenetv2.yaml" >> "${GITHUB_ENV}"
              ;;
            *coreml*)
              echo "USE_COREML_DELEGATE=1" >> "${GITHUB_ENV}"
              ;;
          esac
      - name: Install brew dependencies
        run: |
          # Install dependencies
          brew install libtool
      - name: Install conda and dependencies
        run: |
          # Install conda, setup-miniconda messes with the path that messes with the ruby stuff we do later on
          curl --retry 3 -o "${RUNNER_TEMP}/conda.sh" https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
          chmod +x "${RUNNER_TEMP}/conda.sh"
          /bin/bash "${RUNNER_TEMP}/conda.sh" -b -p "${RUNNER_TEMP}/anaconda"
          echo "${RUNNER_TEMP}/anaconda/bin" >> "${GITHUB_PATH}"
          # shellcheck disable=SC1091
          source "${RUNNER_TEMP}/anaconda/bin/activate"
          conda install -y \
            cffi \
            cmake \
            mkl \
            mkl-include \
            ninja \
            numpy \
            pyyaml \
            requests \
            setuptools \
            typing_extensions
      - name: Run Fastlane
        run: |
          set -x
          cd ios/TestApp
          # install fastlane
          sudo gem install bundler && bundle install
          # install certificates
          echo "${IOS_CERT_KEY_2022}" >> cert.txt
          base64 --decode cert.txt -o Certificates.p12
          rm cert.txt
          bundle exec fastlane install_root_cert
          bundle exec fastlane install_dev_cert
          # install the provisioning profile
          PROFILE=PyTorch_CI_2022.mobileprovision
          PROVISIONING_PROFILES=~/Library/MobileDevice/Provisioning\ Profiles
          mkdir -pv "${PROVISIONING_PROFILES}"
          cd "${PROVISIONING_PROFILES}"
          echo "${IOS_SIGN_KEY_2022}" >> cert.txt
          base64 --decode cert.txt -o ${PROFILE}
          rm cert.txt
      - name: Build
        run: |
          # shellcheck disable=SC1091
          source "${RUNNER_TEMP}/anaconda/bin/activate"
          export TCLLIBPATH="/usr/local/lib"
          python -VV
          export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname "$(which conda)")/../"}
          scripts/build_ios.sh
      - name: Run Build Test
        run: |
          PROFILE=PyTorch_CI_2022
          # run the ruby build script
          if ! [ -x "$(command -v xcodebuild)" ]; then
            echo 'Error: xcodebuild is not installed.'
            exit 1
          fi
          if [ "${IOS_PLATFORM}" != "SIMULATOR" ]; then
            ruby scripts/xcode_build.rb -i build_ios/install -x ios/TestApp/TestApp.xcodeproj -p "${IOS_PLATFORM}" -c "${PROFILE}" -t "${IOS_DEV_TEAM_ID}"
          else
            ruby scripts/xcode_build.rb -i build_ios/install -x ios/TestApp/TestApp.xcodeproj -p "${IOS_PLATFORM}"
          fi

concurrency:
  group: ios-12-5-1-arm64-metal-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
  cancel-in-progress: true
