parameters:
- name: componentName
  type: string
  default: hip_clr_combined
- name: checkoutRepo
  type: string
  default: 'self'
- name: checkoutRef
  type: string
  default: ''
# monorepo related parameters
- name: sparseCheckoutDir
  type: string
  default: ''
- name: triggerDownstreamJobs
  type: boolean
  default: false
- name: downstreamAggregateNames
  type: string
  default: ''
- name: buildDependsOn
  type: object
  default: null
- name: unifiedBuild
  type: boolean
  default: false
# set to true if doing full build of ROCm stack
# and dependencies are pulled from same pipeline
- name: aggregatePipeline
  type: boolean
  default: false
- name: aptPackages
  type: object
  default:
    - cmake
    - libnuma-dev
    - mesa-common-dev
    - ninja-build
    - ocl-icd-libopencl1
    - ocl-icd-opencl-dev
    - opencl-headers
    - zlib1g-dev
- name: pipModules
  type: object
  default:
    - CppHeaderParser
- name: rocmDependenciesAMD
  type: object
  default:
    - llvm-project
    - ROCR-Runtime
    - rocprofiler-register
- name: rocmDependenciesNvidia
  type: object
  default:
    - llvm-project
    - ROCR-Runtime

- name: jobMatrix
  type: object
  default:
    buildJobs:
      - { os: ubuntu2204, packageManager: apt, platform: amd }
      - { os: ubuntu2204, packageManager: apt, platform: nvidia }
      - { os: almalinux8, packageManager: dnf, platform: amd }
      - { os: almalinux8, packageManager: dnf, platform: nvidia }

jobs:
- ${{ each job in parameters.jobMatrix.buildJobs }}:
  - job: ${{ parameters.componentName }}_${{ job.os }}_${{ job.platform }}
    ${{ if parameters.buildDependsOn }}:
      dependsOn:
        - ${{ each build in parameters.buildDependsOn }}:
          - ${{ build }}_${{ job.os }}
    pool:
      vmImage: 'ubuntu-22.04'
    ${{ if eq(job.os, 'almalinux8') }}:
      container:
        image: rocmexternalcicd.azurecr.io/manylinux228:latest
        endpoint: ContainerService3
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        packageManager: ${{ job.packageManager }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    # full checkout of rocm-systems superrepo, we need clr, hip, and hipother
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
        # sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
        os: ${{ job.os }}
        ${{ if eq(job.platform, 'amd') }}:
          dependencyList: ${{ parameters.rocmDependenciesAMD }}
        ${{ elseif eq(job.platform, 'nvidia') }}:
          dependencyList: ${{ parameters.rocmDependenciesNvidia }}
        ${{ if parameters.triggerDownstreamJobs }}:
          downstreamAggregateNames: ${{ parameters.downstreamAggregateNames }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        componentName: clr
        cmakeBuildDir: $(Agent.BuildDirectory)/s/projects/clr/build
        cmakeSourceDir: $(Agent.BuildDirectory)/s/projects/clr
        os: ${{ job.os }}
        useAmdclang: false
        extraBuildFlags: >-
          -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm
          -DROCM_PATH=$(Agent.BuildDirectory)/rocm
          -DHIPCC_BIN_DIR=$(Agent.BuildDirectory)/rocm/bin
          -DHIP_COMMON_DIR=$(Agent.BuildDirectory)/s/projects/hip
          -DHIPNV_DIR=$(Agent.BuildDirectory)/s/projects/hipother/hipnv
          -DHIP_PLATFORM=${{ job.platform }}
          -DCLR_BUILD_HIP=ON
          -DCLR_BUILD_OCL=ON
          -GNinja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        artifactName: ${{ job.platform }}
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        artifactName: ${{ job.platform }}
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-links.yml
