parameters:
  package: ''
  spec: ''
  jobDesc: ''
  packageDesc: ''
  msagent: true
  cpuEnabled: true
  cudaEnabled: true
  condaEnabled: true
  wheelsEnabled: true
  override: false

jobs:
- job: 'Windows_${{ parameters.spec }}_${{ parameters.package }}_Build'
  timeoutInMinutes: 240
  cancelTimeoutInMinutes: 5
  condition: >
    or(and(eq('${{ parameters.package }}', 'Conda'), eq('${{ parameters.spec }}', 'CPU'),
           eq('${{ parameters.condaEnabled }}', 'true'), eq('${{ parameters.cpuEnabled }}', 'true')),
       and(eq('${{ parameters.package }}', 'Wheels'), eq('${{ parameters.spec }}', 'CPU'),
           eq('${{ parameters.wheelsEnabled }}', 'true'), eq('${{ parameters.cpuEnabled }}', 'true')),
       and(eq('${{ parameters.package }}', 'Conda'), eq('${{ parameters.spec }}', 'CUDA'),
           eq('${{ parameters.condaEnabled }}', 'true'), eq('${{ parameters.cudaEnabled }}', 'true')),
       and(eq('${{ parameters.package }}', 'Wheels'), eq('${{ parameters.spec }}', 'CUDA'),
           eq('${{ parameters.wheelsEnabled }}', 'true'), eq('${{ parameters.cudaEnabled }}', 'true')))
  variables:
    - ${{ if eq(parameters.override, 'true') }}:
      - name: PYTORCH_BUILD_VERSION
        value: '1.1.0'
      - name: PYTORCH_BUILD_NUMBER
        value: 1
      - name: PYTORCH_REPO
        value: 'pytorch'
      - name: PYTORCH_BRANCH
        value: 'v1.1.0'
      - name: TORCH_CONDA_BUILD_FOLDER
        value: 'pytorch-1.1.0'
    - ${{ if eq(parameters.msagent, 'true') }}:
      - name: USE_SCCACHE
        value: 0
    - ${{ if eq(parameters.msagent, 'false') }}:
      - name: USE_SCCACHE
        value: 1
    - ${{ if eq(parameters.package, 'Conda') }}:
      - group: peterjc_anaconda_token
      - name: PYTORCH_FINAL_PACKAGE_DIR
        value: '$(Build.Repository.LocalPath)\windows\output'

  strategy:
    maxParallel: 10
    matrix:
      ${{ if eq(parameters.spec, 'CPU') }}:
        PY3.6:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: cpu
        PY3.7:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: cpu
        PY3.8:
          DESIRED_PYTHON: 3.8
          CUDA_VERSION: cpu
      ${{ if and(eq(parameters.package, 'Wheels'), eq(parameters.spec, 'CPU')) }}:
        LIBTORCH:
          DESIRED_PYTHON: 3
          CUDA_VERSION: cpu
          BUILD_PYTHONLESS: 1
        LIBTORCH_DEBUG:
          DESIRED_PYTHON: 3
          CUDA_VERSION: cpu
          BUILD_PYTHONLESS: 1
          DEBUG: 1
      ${{ if ne(parameters.spec, 'CPU') }}:
        PY3.6_92:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: 92
        PY3.7_92:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: 92
        PY3.8_92:
          DESIRED_PYTHON: 3.8
          CUDA_VERSION: 92
        PY3.6_101:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: 101
        PY3.7_101:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: 101
        PY3.8_101:
          DESIRED_PYTHON: 3.8
          CUDA_VERSION: 101
        PY3.6_102:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: 102
        PY3.7_102:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: 102
        PY3.8_102:
          DESIRED_PYTHON: 3.8
          CUDA_VERSION: 102
        PY3.6_110:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: 110
        PY3.7_110:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: 110
        PY3.8_110:
          DESIRED_PYTHON: 3.8
          CUDA_VERSION: 110
      ${{ if and(ne(parameters.spec, 'CPU'), eq(parameters.package, 'Wheels')) }}:
        LIBTORCH_92:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 92
        LIBTORCH_92_DEBUG:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 92
          DEBUG: 1
        LIBTORCH_101:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 101
        LIBTORCH_101_DEBUG:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 101
          DEBUG: 1
        LIBTORCH_102:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 102
        LIBTORCH_102_DEBUG:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 102
          DEBUG: 1
        LIBTORCH_110:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 110
        LIBTORCH_110_DEBUG:
          DESIRED_PYTHON: 3
          BUILD_PYTHONLESS: 1
          CUDA_VERSION: 110
          DEBUG: 1

  pool:
    ${{ if eq(parameters.msagent, 'true') }}:
      vmImage: 'vs2017-win2016'
    ${{ if eq(parameters.msagent, 'false') }}:
      name: 'pytorch'

  steps:
  - script: |
      taskkill /im nvcc.exe /f || ver > NUL
      taskkill /im sccache.exe /f || ver > NUL
      taskkill /im cl.exe /f || ver > NUL
      taskkill /im ninja.exe /f || ver > NUL
      taskkill /im link.exe /f || ver > NUL
      taskkill /im cmake.exe /f || ver > NUL
      taskkill /im conda-build.exe /f || ver > NUL
      rd /s /q $(Build.Repository.LocalPath)\windows\pytorch || ver > NUL
      rd /s /q $(Build.Repository.LocalPath)\windows\output || ver > NUL

    displayName: 'Cleaning unclosed processes & files'

  - checkout: self
    clean: true

  - template: setup_env_for_msagent.yml
    parameters:
      msagent: ${{ parameters.msagent }}

  - template: setup_nightly_variables.yml
    parameters:
      package: ${{ parameters.package }}

  - ${{ if eq(parameters.package, 'Wheels') }}:
    - template: build_wheels.yml
      parameters:
        msagent: ${{ parameters.msagent }}

  - ${{ if eq(parameters.package, 'Conda') }}:
    - template: build_conda.yml
      parameters:
        msagent: ${{ parameters.msagent }}

  - ${{ if or(eq(parameters.package, 'Wheels'), eq(parameters.package, 'Conda')) }}:
    - template: publish_test_results.yml
      parameters:
        msagent: ${{ parameters.msagent }}

  - ${{ if and(eq(parameters.package, 'Wheels'), eq(parameters.spec, 'CPU')) }}:
    - template: upload_to_s3.yml
      parameters:
        cuVer: '$(CUDA_VERSION)'
        cudaVer: '$(CUDA_VERSION)'

  - ${{ if and(eq(parameters.package, 'Wheels'), ne(parameters.spec, 'CPU')) }}:
    - template: upload_to_s3.yml
      parameters:
        cuVer: 'cu$(CUDA_VERSION)'
        cudaVer: 'cuda$(CUDA_VERSION)'

  - ${{ if eq(parameters.package, 'Conda') }}:
    - template: upload_to_conda.yml
      parameters:
        user: $(peterjc_conda_username)
        pass: $(peterjc_conda_password)

  # If you want to upload binaries to Azure Git, please uncomment this section.
  # - ${{ if or(eq(parameters.package, 'Wheels'), eq(parameters.package, 'Conda')) }}:
  #   - template: publish_test_results.yml
  #     parameters:
  #       msagent: ${{ parameters.msagent }}
  #   - template: publish_packages.yml
  #     parameters:
  #       package: ${{ parameters.package }}
