# This file contains the ADO job that build so-file on Linux
parameters:
- name: IsReleaseBuild
  displayName: "Is this a release build?"
  type: boolean
  default: false

- name: OrtExtensionsArch
  type: string

- name: PoolName
  type: string
  default: 'aiinfra-Linux-CPU'

- name: OrtExtensionsCFlags
  type: string

- name: OrtExtensionsCXXFlags
  type: string

- name: AdditionalBuildFlags
  displayName: Additional build flags for build_lib.sh
  type: string

jobs:
- job: Linux_C_API_Packaging_CPU_${{parameters.OrtExtensionsArch}}

  workspace:
    clean: all
  timeoutInMinutes:  210
  pool: ${{parameters.PoolName}}
  steps:
    - checkout: self
      clean: true
      submodules: none

    - bash: |
        sudo apt-get update
        sudo apt-get install -y libssh-dev
      displayName: Install OpenSSL for Azure custom ops

    # NOTE: on arm64 machine, CMake version needs to be updated since we now require CMake 3.25 or newer.
    - ${{ if eq(parameters.OrtExtensionsArch, 'x64') }}:
      - bash: |
          export CFLAGS="${{parameters.OrtExtensionsCFlags}}"
          export CXXFLAGS="${{parameters.OrtExtensionsCXXFlags}}"
          ./build_lib.sh --build_dir $(Build.BinariesDirectory)/out/ --config RelWithDebInfo --parallel --enable_cxx_tests ${{parameters.AdditionalBuildFlags}}
        workingDirectory: '$(Build.SourcesDirectory)'
        displayName: 'build onnxruntime-extensions and run tests'
    - ${{ else }}:
      - bash: |
          sudo apt remove cmake
          pip install cmake --upgrade
          export PATH=~/.local/bin:$PATH
          cmake --version
          export CFLAGS="${{parameters.OrtExtensionsCFlags}}"
          export CXXFLAGS="${{parameters.OrtExtensionsCXXFlags}}"
          ./build_lib.sh --build_dir $(Build.BinariesDirectory)/out/ --config RelWithDebInfo --parallel --enable_cxx_tests ${{parameters.AdditionalBuildFlags}}
        workingDirectory: '$(Build.SourcesDirectory)'
        displayName: 'build onnxruntime-extensions'

    - template: set-package-version-variable-step.yml
      parameters:
        IsReleaseBuild: true
        PackageVersionVariableName: ORT_EXTENSIONS_VERSION

    - bash: |
       set -e -x

       lib_target=$(Build.BinariesDirectory)/onnxruntime-extensions-linux-${{parameters.OrtExtensionsArch}}-$(ORT_EXTENSIONS_VERSION)/lib
       mkdir -p $lib_target

       ls -l out/RelWithDebInfo/lib
       cp out/RelWithDebInfo/lib/libortextensions.so $lib_target

       cd $lib_target
       objcopy --only-keep-debug libortextensions.so libortextensions.so.dbg
       objcopy --strip-debug libortextensions.so
       objcopy --add-gnu-debuglink=libortextensions.so.dbg libortextensions.so

      displayName: create and copy artifacts.
      workingDirectory: $(Build.BinariesDirectory)


    - task: ArchiveFiles@2
      inputs:
        rootFolderOrFile: '$(Build.BinariesDirectory)/onnxruntime-extensions-linux-${{parameters.OrtExtensionsArch}}-$(ORT_EXTENSIONS_VERSION)'
        includeRootFolder: true
        archiveType: 'tar' # Options: zip, 7z, tar, wim
        tarCompression: 'gz'
        archiveFile: '$(Build.ArtifactStagingDirectory)/onnxruntime-extensions-linux-${{parameters.OrtExtensionsArch}}.tgz'
        replaceExistingArchive: true

    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Build.ArtifactStagingDirectory)'
        artifactName: 'onnxruntime-extensions-linux-${{parameters.OrtExtensionsArch}}'

    - task: mspremier.PostBuildCleanup.PostBuildCleanup-task.PostBuildCleanup@3
      displayName: 'Clean Agent Directories'
      condition: always()
