parameters:
- name: 'toolset'
  type: string
  values:
  - make
  - make_custom_boost_version
  - make_trace
  - ninja
  - msbuild
  - make_homebrew_macos
- name: 'custom_boost_version_url'
  type: string
  default: 'https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.bz2'
- name: release_binaries
  type: boolean
  default: false

jobs:
- job: build_ctest_and_upload_uhd_${{ parameters.toolset }}
  displayName: ${{ parameters.toolset }}, ctest, and upload UHD
  dependsOn: get_latest_uhd_docker
  variables:
    # Docker vars from job-get-latest-uhd-docker.yml
    dockerBuildNumber: $[ dependencies.get_latest_uhd_docker.outputs['setDockerVar.dockerBuildNumber'] ]
    dockerImagePrefix: $[ dependencies.get_latest_uhd_docker.outputs['setDockerVar.dockerImagePrefix'] ]
    dockerImageMatrixLin: $[ dependencies.get_latest_uhd_docker.outputs['setDockerVar.dockerImageMatrixLin'] ]
    dockerImageMatrixWin: $[ dependencies.get_latest_uhd_docker.outputs['setDockerVar.dockerImageMatrixWin'] ]
    dockerImageMatrixUbuntuDeb: $[ dependencies.get_latest_uhd_docker.outputs['setDockerVar.dockerImageMatrixUbuntuDeb'] ]
    macOSBuilders: $[ dependencies.get_latest_uhd_docker.outputs['setDockerVar.macOSBuilders'] ]
  pool:
    ${{ if eq(parameters.toolset, 'msbuild') }}:
      name: de-dre-lab
    ${{ if ne(parameters.toolset, 'msbuild') }}:
      name: Drivers-NIBuildFarm-RFMIBUILD
    demands:
      - ${{ if eq(parameters.toolset, 'msbuild') }}:
        - docker
        - Agent.OS -equals Windows_NT
      - ${{ if or(eq(parameters.toolset, 'make'), eq(parameters.toolset, 'make_trace'), eq(parameters.toolset, 'ninja'), eq(parameters.toolset, 'make_custom_boost_version')) }}:
        - docker
        - Agent.OS -equals Linux
      - ${{ if eq(parameters.toolset, 'make_homebrew_macos') }}:
        - Agent.OS -equals Darwin
        - MACOS_MAJOR_VERSION -equals $(macOSVersionDemand)
        - Agent.OSArchitecture -equals $(macOSArchDemand)
  workspace:
    clean: outputs
  strategy:
    ${{ if eq(parameters.toolset, 'msbuild') }}:
      matrix: $[ variables.dockerImageMatrixWin ]
    ${{ if or(eq(parameters.toolset, 'make'), eq(parameters.toolset, 'make_trace'), eq(parameters.toolset, 'ninja'), eq(parameters.toolset, 'make_custom_boost_version')) }}:
      matrix: $[ variables.dockerImageMatrixLin ]
    ${{ if eq(parameters.toolset, 'make_homebrew_macos') }}:
      matrix: $[ variables.macOSBuilders ]
  ${{ if ne(parameters.toolset, 'make_homebrew_macos') }}:
    container:
      image: '$(dockerImagePrefix)$(DockerImageName):$(dockerBuildNumber)'
      endpoint: 'Docker / Artifactory - rnd-builds-local'
  steps:
  - checkout: self
    clean: true

  - download: uhd_build_docker_container

  - ${{ if eq(parameters.toolset, 'make') }}:
    - template: steps-build-uhd-make.yml
      parameters:
        uhdSrcDir: $(Build.SourcesDirectory)
        uhdBuildDir: $(Build.BinariesDirectory)/uhddev/build
        uhdGenerateTestBinaries: true

  - ${{ if eq(parameters.toolset, 'make_trace') }}:
    - template: steps-build-uhd-make.yml
      parameters:
        uhdSrcDir: $(Build.SourcesDirectory)
        uhdBuildDir: $(Build.BinariesDirectory)/uhddev/build
        uhdEnableTraceLog: true

  - ${{ if eq(parameters.toolset, 'make_homebrew_macos') }}:
    - template: steps-build-uhd-make-homebrew-macos.yml
      parameters:
        uhdSrcDir: $(Build.SourcesDirectory)
        uhdBuildDir: $(Build.BinariesDirectory)/uhddev/build

  - ${{ if eq(parameters.toolset, 'ninja') }}:
    - template: steps-build-uhd-ninja.yml
      parameters:
        uhdSrcDir: $(Build.SourcesDirectory)
        uhdBuildDir: $(Build.BinariesDirectory)/uhddev/build

  - ${{ if eq(parameters.toolset, 'msbuild') }}:
    - template: steps-build-uhd-msbuild.yml
      parameters:
        uhdSrcDir: $(Build.SourcesDirectory)
        uhdBuildDir: $(Build.BinariesDirectory)\\uhddev\\build
        uhdImageDir: $(Build.BinariesDirectory)\\uhd-images
        uhdReleaseBinaries: ${{ parameters.release_binaries }}
        cmakeCompiler: $(cmakeCompiler)
        cmakeArch: $(cmakeArch)
        vsArch: $(vsArch)
        vsYear: $(vsYear)

  - ${{ if eq(parameters.toolset, 'make_custom_boost_version') }}:
    - template: steps-build-uhd-make.yml
      parameters:
        uhdSrcDir: $(Build.SourcesDirectory)
        uhdBuildDir: $(Build.BinariesDirectory)/uhddev/build
        uhdCustomBoostPackage: true
        uhdCustomBoostPackageURL: ${{ parameters.custom_boost_version_url }}

  - task: ArchiveFiles@2
    inputs:
      rootFolderOrFile: $(Build.BinariesDirectory)
      includeRootFolder: false
      archiveType: tar
      tarCompression: gz
      archiveFile: $(Build.ArtifactStagingDirectory)/uhddev-$(buildOSName)-${{ parameters.toolset }}.tar.gz
      replaceExistingArchive: true
    displayName: Compress build files

  - task: CopyFiles@2
    inputs:
      sourceFolder: $(Pipeline.Workspace)/uhd_build_docker_container/$(UHDPipelineDockerImageArtifactName)
      targetFolder: $(Build.ArtifactStagingDirectory)
    displayName:  Copy builder container version to artifact folder

  - task: PublishPipelineArtifact@1
    inputs:
      targetPath: '$(Build.ArtifactStagingDirectory)'
      artifactName: 'uhddev-$(buildOSName)-${{ parameters.toolset }}'
    displayName: Upload uhd build artifacts

  - task: CopyFiles@2
    inputs:
      sourceFolder: $(Build.BinariesDirectory)/uhddev/
      contents: build/Testing/**
      targetFolder: $(Common.TestResultsDirectory)
    displayName: Copy ${{ parameters.toolset }} ctest results

  - task: PublishTestResults@2
    inputs:
      testResultsFormat: cTest
      testResultsFiles: $(Common.TestResultsDirectory)/build/Testing/**/Test.xml
      failTaskOnFailedTests: true
      testRunTitle: uhd ctest $(buildOSName)-${{ parameters.toolset }}
    displayName: Publish $(buildOSName)-${{ parameters.toolset }} ctest results
