parameters:
- name: componentName
  type: string
  default: rocprofiler-compute
- 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
    - libdw-dev
    - libtbb-dev
    - locales
    - ninja-build
    - python3-pip
- name: pipModules
  type: object
  default:
    - astunparse==1.6.2
    - colorlover
    - dash-bootstrap-components
    - dash-svg
    - "dash>=3.0.0"
    - kaleido==0.2.1
    - matplotlib
    - "numpy>=1.17.5"
    - "pandas>=1.4.3"
    - plotext
    - plotille
    - pymongo
    - pyyaml
    - setuptools
    - sqlalchemy
    - tabulate
    - textual
    - textual_plotext
    - textual-fspicker
    - tqdm
    - mock
    - pytest
    - pytest-cov
    - pytest-xdist
- name: rocmTestDependencies
  type: object
  default:
    - amdsmi
    - clr
    - llvm-project
    - rocm-cmake
    - rocm-core
    - rocminfo
    - rocm_smi_lib
    - ROCR-Runtime
    - rocprofiler
    - rocprofiler-sdk
    - rocprofiler-register
    - roctracer

- name: jobMatrix
  type: object
  default:
    buildJobs:
      - gfx942:
        target: gfx942
      - gfx90a:
        target: gfx90a
    testJobs:
      - gfx942:
        target: gfx942
      - gfx90a:
        target: gfx90a

jobs:
- ${{ each job in parameters.jobMatrix.buildJobs }}:
  - job: rocprofiler_compute_build_${{ job.target }}
    ${{ if parameters.buildDependsOn }}:
      dependsOn:
        - ${{ each build in parameters.buildDependsOn }}:
          - ${{ build }}_${{ job.os }}_${{ job.target }}
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    pool:
      vmImage: ${{ variables.BASE_BUILD_POOL }}
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
        sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        extraBuildFlags: >-
          -GNinja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-links.yml
    # - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
    #   parameters:
    #     aptPackages: ${{ parameters.aptPackages }}
    #     pipModules: ${{ parameters.pipModules }}
    #     gpuTarget: ${{ job.target }}

- ${{ if eq(parameters.unifiedBuild, False) }}:
  - ${{ each job in parameters.jobMatrix.testJobs }}:
    - job: rocprofiler_compute_test_${{ job.target }}
      timeoutInMinutes: 120
      dependsOn: rocprofiler_compute_build_${{ job.target }}
      condition:
        and(succeeded(),
          eq(variables['ENABLE_${{ upper(job.target) }}_TESTS'], 'true'),
          not(containsValue(split(variables['DISABLED_${{ upper(job.target) }}_TESTS'], ','), '${{ parameters.componentName }}')),
          eq(${{ parameters.aggregatePipeline }}, False)
        )
      variables:
      - group: common
      - template: /.azuredevops/variables-global.yml
      - name: PYTHON_VERSION
        value: 3.10
      pool: ${{ job.target }}_test_pool
      workspace:
        clean: all
      steps:
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
        parameters:
          aptPackages: ${{ parameters.aptPackages }}
          pipModules: ${{ parameters.pipModules }}
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
        parameters:
          checkoutRepo: ${{ parameters.checkoutRepo }}
          sparseCheckoutDir: ${{ parameters.sparseCheckoutDir }}
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/local-artifact-download.yml
        parameters:
          preTargetFilter: ${{ parameters.componentName }}
          gpuTarget: ${{ job.target }}
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-aqlprofile.yml
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
        parameters:
          checkoutRef: ${{ parameters.checkoutRef }}
          dependencyList: ${{ parameters.rocmTestDependencies }}
          gpuTarget: ${{ job.target }}
          ${{ if parameters.triggerDownstreamJobs }}:
            downstreamAggregateNames: ${{ parameters.downstreamAggregateNames }}
      - task: Bash@3
        displayName: Add en_US.UTF-8 locale
        inputs:
          targetType: inline
          script: |
            sudo locale-gen en_US.UTF-8
            sudo update-locale
            locale -a
      - task: Bash@3
        displayName: Add ROCm binaries to PATH
        inputs:
          targetType: inline
          script: |
            echo "##vso[task.prependpath]$(Agent.BuildDirectory)/rocm/bin"
            echo "##vso[task.prependpath]$(Agent.BuildDirectory)/rocm/llvm/bin"
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
        parameters:
          extraBuildFlags: >-
            -DCMAKE_HIP_ARCHITECTURES=${{ job.target }}
            -DCMAKE_C_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/amdclang
            -DCMAKE_MODULE_PATH=$(Agent.BuildDirectory)/rocm/lib/cmake/hip
            -DCMAKE_PREFIX_PATH=$(Agent.BuildDirectory)/rocm
            -DROCM_PATH=$(Agent.BuildDirectory)/rocm
            -DCMAKE_BUILD_TYPE=Release
            -DENABLE_TESTS=ON
            -DINSTALL_TESTS=ON
            -GNinja
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/gpu-diagnostics.yml
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
        parameters:
          componentName: ${{ parameters.componentName }}
          testDir: $(Build.BinariesDirectory)/libexec/rocprofiler-compute
          testExecutable: ROCM_PATH=$(Agent.BuildDirectory)/rocm ctest
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
        parameters:
          aptPackages: ${{ parameters.aptPackages }}
          pipModules: ${{ parameters.pipModules }}
          environment: test
          gpuTarget: ${{ job.target }}
