parameters:
  - name: Artifacts
    type: object
    default: []
  - name: ServiceDirectory
    type: string
    default: not-specified
  - name: CMakeTestOptions
    type: object
    default: []
  - name: MaxParallel
    type: number
    default: 0
  # Matrix generation:
  # https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/scripts/job-matrix/README.md
  - name: Matrix
    type: string
    default: ''
  - name: UsePlatformContainer
    type: boolean
    default: false
  - name: DependsOn
    type: string
    default: not-set
  - name: CloudConfig
    type: object
    default: {}
  - name: JobName
    type: string
    default: CMakeGenerate
  - name: TimeoutInMinutes
    type: number
    default: 120
  - name: OSName
    type: string
    default: ''

jobs:
- job:
  displayName: ${{ parameters.JobName }}
  timeoutInMinutes: ${{ parameters.TimeoutInMinutes }}
  condition: >-
    and(
      succeededOrFailed(),
      ne(variables['Skip.CMakeGenerate'], 'true'),
      ne(${{ parameters.Matrix }}, '{}')
    )
  dependsOn: ${{ parameters.DependsOn }}
  strategy:
    maxParallel: ${{ parameters.MaxParallel }}
    matrix: $[ ${{ parameters.Matrix }} ]

  pool:
    name: $(Pool)
    # 1es pipeline templates converts `image` to demands: ImageOverride under the hood
    # which is incompatible with image selection in the default non-1es hosted pools
    ${{ if eq(parameters.OSName, 'macOS') }}:
      vmImage: $(OSVmImage)
    ${{ else }}:
      image: $(OSVmImage)
    os: ${{ parameters.OSName }}

  ${{ if eq(parameters.UsePlatformContainer, 'true') }}:
    # Add a default so the job doesn't fail when the matrix is empty
    container: $[ variables['Container'] ]

  variables:
    CMOCKA_XML_FILE: "%g-test-results.xml"

  steps:
  - checkout: self
    submodules: recursive

  - template: /eng/common/pipelines/templates/steps/verify-agent-os.yml
    parameters:
      AgentImage: $(OSVmImage)

  - template: /eng/pipelines/templates/steps/fix-1es-image-apt-azure-sources.yml

  - pwsh: sudo apt update && sudo apt install -y $(AptDependencies)
    retryCountOnTaskFailure: 10
    condition: and(succeeded(), ne(variables['AptDependencies'], ''))
    displayName: Install dependencies from apt

  - pwsh: |
      brew install $(BrewDependencies)
    condition: and(succeeded(), ne(variables['BrewDependencies'], ''))
    displayName: Install dependencies from brew

  - template: /eng/pipelines/templates/steps/vcpkg.yml

  - script: cmake --version
    displayName: cmake --version

  - ${{ each artifact in parameters.Artifacts }}:
    - script: echo 'CMake generate tests for ${{ artifact.Name }}'
      displayName: ${{ artifact.Name }}
    - ${{ each cmakeOption in parameters.CMakeTestOptions }}:
      - template: /eng/pipelines/templates/steps/cmake-generate.yml
        parameters:
          CmakeGeneratePath: sdk/${{ parameters.ServiceDirectory }}/${{ artifact.Path }}
          GenerateArgs: ${{ cmakeOption.Value }}
          Env: "$(CmakeEnvArg)"
          PackageName: ${{ artifact.Name }}

  - template: /eng/pipelines/templates/steps/show-failure-logs.yml
