parameters:
- name: componentName
  type: string
  default: MIVisionX
- 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
    - ninja-build
    - wget
    - unzip
    - libstdc++-12-dev
    - pkg-config
    - ffmpeg
    - libavcodec-dev
    - libavformat-dev
    - libavutil-dev
    - libswscale-dev
    - libgmp-dev
    - libomp-dev
    - libopencv-dev
    - protobuf-compiler
    - libprotoc-dev
    - python3-pip
- name: pipModules
  type: object
  default:
    - future==1.0.0
    - pytz==2022.1
    - numpy==1.23
    - google==3.0.0
    - protobuf==3.12.4
    - onnx==1.12.0
    - nnef==1.0.7
- name: rocmDependencies
  type: object
  default:
    - AMDMIGraphX
    - clr
    - half
    - hipBLAS-common
    - hipBLASLt
    - llvm-project
    - MIOpen
    - rocBLAS
    - rocDecode
    - rocm-cmake
    - rocminfo
    - rocprofiler-register
    - ROCR-Runtime
    - rpp
- name: rocmTestDependencies
  type: object
  default:
    - aomp
    - clr
    - half
    - hipBLAS-common
    - hipBLASLt
    - llvm-project
    - MIOpen
    - rocBLAS
    - rocprofiler-register
    - ROCR-Runtime
    - roctracer
    - rpp

- 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: ${{ parameters.componentName }}_build_ubuntu2204_${{ job.target }}
    ${{ if parameters.buildDependsOn }}:
      dependsOn:
        - ${{ each build in parameters.buildDependsOn }}:
          - ${{ build }}_ubuntu2204_${{ job.target }}
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    pool: ${{ variables.MEDIUM_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 }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        gpuTarget: ${{ job.target }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
        ${{ if parameters.triggerDownstreamJobs }}:
          downstreamAggregateNames: ${{ parameters.downstreamAggregateNames }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        extraBuildFlags: >-
          -DCMAKE_BUILD_TYPE=Release
          -DROCM_PATH=$(Agent.BuildDirectory)/rocm
          -DROCM_DEP_ROCMCORE=ON
          -GNinja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        gpuTarget: ${{ job.target }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        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 }}

- ${{ each job in parameters.jobMatrix.testJobs }}:
  - job: ${{ parameters.componentName }}_test_ubuntu2204_${{ job.target }}
    dependsOn: ${{ parameters.componentName }}_build_ubuntu2204_${{ 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: LD_LIBRARY_PATH
      value: $(Agent.BuildDirectory)/rocm/lib:$(Agent.BuildDirectory)/rocm/include/mivisionx/VX
    pool: ${{ job.target }}_test_pool
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/preamble.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/local-artifact-download.yml
      parameters:
        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: Build MIVisionX tests
      inputs:
        targetType: inline
        script: |
          # Assuming that /opt is no longer persistent across runs, test environments are fully ephemeral
          sudo ln -s $(Agent.BuildDirectory)/rocm /opt/rocm
          mkdir mivisionx-tests
          cd mivisionx-tests
          cmake $(Agent.BuildDirectory)/rocm/share/mivisionx/test
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/gpu-diagnostics.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        testDir: 'mivisionx-tests'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        environment: test
        gpuTarget: ${{ job.target }}
        optSymLink: true
