#######
# Linux
#######

jobs:
- job: Linux
  pool:
    vmImage: 'ubuntu-latest'
  
  strategy:
    matrix:
      py38:
        python.version: '3.8'
    maxParallel: 1

  steps:
  - task: DownloadGitHubRelease@0
    inputs:
      connection: 'microsoft'
      userRepository: 'microsoft/onnxruntime'
      defaultVersionType: 'specificVersion'
      version: '34858191'
      itemPattern: '*-linux-x64-1.6.0*'
      downloadPath: '$(Build.sourcesdirectory)'
    displayName: Download the ONNXRuntime prebuilt package.

  - task: ExtractFiles@1
    inputs:
      archiveFilePatterns: '**/*.tgz'
      destinationFolder: '$(Build.sourcesdirectory)'
      cleanDestinationFolder: false
      overwriteExistingFiles: true
    displayName: Unpack ONNXRuntime package.

  - script: |
      sh ./build.sh -DONNXRUNTIME_LIB_DIR=onnxruntime-linux-x64-1.6.0/lib
    displayName: build the customop library with onnxruntime

  - script: |
      cd out/Linux
      ctest -C RelWithDebInfo
    displayName: Run C++ native tests

  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'
      addToPath: true

  - script: |
      python -m pip install --upgrade pip
      python -m pip install --upgrade setuptools
      python -m pip install -r requirements.txt
    displayName: Install requirements.txt

  - script: |
      python setup.py develop
    displayName: Build the library and tests

  - script: python -m pip install torch==1.7.1+cpu torchvision==0.8.2+cpu torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
    displayName: Install pytorch

  - script: python -m pip install -r requirements-dev.txt
    displayName: Install requirements-dev.txt

  - script: python -m pytest test --verbose --verbose 
    displayName: Run python test

#######
# macOS
#######

- job: macOS
  pool:
    vmImage: 'macOS-latest'

  strategy:
    matrix:
      py38:
        python.version: '3.8'
    maxParallel: 1

  steps:
  # needed for onnxruntime
  - script: brew install libomp
    displayName: 'Install omp'

  - task: DownloadGitHubRelease@0
    inputs:
      connection: 'microsoft'
      userRepository: 'microsoft/onnxruntime'
      defaultVersionType: 'specificVersion'
      version: '34858191'
      itemPattern: '*-osx-x64-1.6.0*'
      downloadPath: '$(Build.sourcesdirectory)'
    displayName: Download the ONNXRuntime prebuilt package.

  - task: ExtractFiles@1
    inputs:
      archiveFilePatterns: '**/*.tgz'
      destinationFolder: '$(Build.sourcesdirectory)'
      cleanDestinationFolder: false
      overwriteExistingFiles: true
    displayName: Unpack ONNXRuntime package.

  - script: |
      sh ./build.sh -DONNXRUNTIME_LIB_DIR=onnxruntime-osx-x64-1.6.0/lib
    displayName: build the customop library with onnxruntime

  - script: |
      cd out/Darwin
      ctest -C RelWithDebInfo
    displayName: Run C++ native tests

  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'
      addToPath: true

  - script: |
      python -m pip install --upgrade pip
      python -m pip install --upgrade setuptools
      python -m pip install -r requirements.txt
    displayName: Install requirements.txt

  - script: python -c "import onnxruntime;print(onnxruntime.__version__)"
    displayName: Check installation

  - script: |
      call activate pyenv
      python setup.py develop
    displayName: Build the library and tests

  - script: python -m pip install -r requirements-dev.txt
    displayName: Install requirements-dev.txt

  - script: python -m pip install torch torchvision torchaudio
    displayName: Install pytorch

  - script: python -m pytest test --verbose
    displayName: Run python test

#########
# Windows
#########

- job: Windows
  pool:
    vmImage: 'windows-latest'
  steps:
  - task: DownloadGitHubRelease@0
    inputs:
      connection: 'microsoft'
      userRepository: 'microsoft/onnxruntime'
      defaultVersionType: 'specificVersion'
      version: '34858191'
      itemPattern: '*-win-x64-1.6.0*'
      downloadPath: '$(Build.sourcesdirectory)'
    displayName: Download the ONNXRuntime prebuilt package.

  - task: ExtractFiles@1
    inputs:
      archiveFilePatterns: '**/*.zip'
      destinationFolder: '$(Build.sourcesdirectory)'
      cleanDestinationFolder: false
      overwriteExistingFiles: true
    displayName: Unpack ONNXRuntime package.

  - script: |
      call .\build.bat -DONNXRUNTIME_LIB_DIR=.\onnxruntime-win-x64-1.6.0\lib
    displayName: build the customop library with onnxruntime

  - script: |
      cd out/Windows
      ctest -C RelWithDebInfo
    displayName: Run C++ native tests

  - powershell: Write-Host "##vso[task.prependpath]$env:CONDA\Scripts"
    displayName: Add conda to PATH

  - script: conda create --yes --quiet --name pyenv -c conda-forge python=3.8 numpy
    displayName: Create Anaconda environment

  - script: |
      call activate pyenv
      python -m pip install --upgrade pip
      python -m pip install -r requirements.txt
      python -m pip install -r requirements-dev.txt
    displayName: Install requirements.txt

  - script: |
      call activate pyenv
      python -m pip install torch==1.7.1+cpu torchvision==0.8.2+cpu torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
    displayName: Install pytorch

  - script: |
      call activate pyenv
      echo Test numpy installation... && python -c "import numpy"
      python setup.py develop
    displayName: Build the custom-op library

  - script: |
      call activate pyenv
      python -m pytest test
    displayName: Run python test
