# packaging pipeline for onnxruntime-extensions ios cocoapods package
parameters:
- name: Platform
  displayName: ios Platform
  type: string

- name: IosArch
  displayName: IosArch
  type: string

- name: IsReleaseBuild
  displayName: "Is this a release build?"
  type: boolean


- name: AdditionalBuildFlags
  displayName: Additional build flags for /tools/ios/build_xcframework.py
  type: string

jobs:
  - job: IOS_C_API_Packaging_${{ parameters.Platform }}_${{ parameters.IosArch }}
    condition: succeeded()
    pool:
      vmImage: "macOS-13"
    timeoutInMinutes: 120
    variables:
      buildConfig: Release
    steps:
      - template: use-xcode-version.yml

      - task: UsePythonVersion@0
        inputs:
          versionSpec: "3.9"
          addToPath: true
          architecture: "x64"

      # iOS xcframework build doesn't work with CMake 3.25.1, pin to 3.25.0
      - script: |
          python -m pip install cmake==3.25.0
        displayName: "Install CMake 3.25.0"

      - template: set-package-version-variable-step.yml
        parameters:
          IsReleaseBuild: ${{ parameters.IsReleaseBuild }}
          PackageVersionVariableName: ORT_EXTENSIONS_POD_VERSION

      - script: |
          python ./tools/ios/build_xcframework.py \
            --output_dir $(Build.BinariesDirectory)/xcframework_out \
            --config $(buildConfig) \
            --platform_arch  ${{ parameters.Platform }} ${{ parameters.IosArch }} \
            --mode build_platform_arch_frameworks_only \
            -- \
            ${{parameters.AdditionalBuildFlags}}
        displayName: "Build xcframework"

      - bash: |
          set -e -x

          package_name=onnxruntime-extensions-ios-${{ parameters.Platform }}-${{ parameters.IosArch }}-lib.tgz
          target_folder=xcframework_out/intermediates/${{ parameters.Platform }}/${{ parameters.IosArch }}/$(buildConfig)
          framework_info=${target_folder}/framework_info.json
          tar -cvzf $(Build.ArtifactStagingDirectory)/${package_name} -C $(Build.BinariesDirectory)/ ${target_folder}/static_framework ${framework_info}

        displayName: "Assemble artifacts"

      - publish: "$(Build.ArtifactStagingDirectory)"
        artifact: onnxruntime-extensions-ios-${{ parameters.Platform }}-${{ parameters.IosArch }}-lib
        displayName: "Publish artifacts"
