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: 60
  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: TORCHVISION_BUILD_NUMBER
        value: 1
      - name: PYTORCH_REPO
        value: 'pytorch'
      - name: PYTORCH_BRANCH
        value: 'v0.4.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)\packaging\windows\output'
      
  strategy:
    maxParallel: 10
    matrix:
      ${{ if eq(parameters.spec, 'CPU') }}:
        PY3.5:
          DESIRED_PYTHON: 3.5
          CUDA_VERSION: cpu
        PY3.6:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: cpu
        PY3.7:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: cpu
      ${{ if ne(parameters.spec, 'CPU') }}:
        # PY3.5_92:
        #   DESIRED_PYTHON: 3.5
        #   CUDA_VERSION: 92
        # PY3.6_92:
        #   DESIRED_PYTHON: 3.6
        #   CUDA_VERSION: 92
        # PY3.7_92:
        #   DESIRED_PYTHON: 3.7
        #   CUDA_VERSION: 92
        PY3.5_100:
          DESIRED_PYTHON: 3.5
          CUDA_VERSION: 100
        PY3.6_100:
          DESIRED_PYTHON: 3.6
          CUDA_VERSION: 100
        PY3.7_100:
          DESIRED_PYTHON: 3.7
          CUDA_VERSION: 100

  pool:
    ${{ if eq(parameters.msagent, 'true') }}:
      vmImage: 'win1803'
    ${{ if eq(parameters.msagent, 'false') }}:
      name: 'release'

  steps:
  - checkout: self
    clean: true

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

  # - ${{ if and(eq(parameters.override, 'true'),  eq(parameters.package, 'Wheels')) }}:
  #   - template: override_pytorch_version.yml

  - 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 you want to upload binaries to S3 & Anaconda Cloud, please uncomment this section.
  - ${{ 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 }}
