# largely referenced from: https://github.com/ROCm/omnitrace/blob/main/.github/workflows/ubuntu-jammy.yml
parameters:
- name: checkoutRepo
  type: string
  default: 'self'
- name: checkoutRef
  type: string
  default: ''
# 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:
    - autoconf
    - autotools-dev
    - bison
    - build-essential
    - bzip2
    - clang
    - cmake
    - chrpath
    - environment-modules
    - ffmpeg
    - g++-12
    - libavcodec-dev
    - libavformat-dev
    - libavutil-dev
    - libdrm-amdgpu-dev
    - libdrm-dev
    - libdw-dev
    - libfabric-dev
    - libiberty-dev
    - libpapi-dev
    - libpfm4-dev
    - libtool
    - libopenmpi-dev
    - libsqlite3-dev
    - m4
    - ninja-build
    - openmpi-bin
    - pkg-config
    - python3-pip
    - software-properties-common
    - texinfo
    - zlib1g-dev
- name: pipModules
  type: object
  default:
    - numpy
    - perfetto
    - dataclasses
- name: rocmDependencies
  type: object
  default:
    - amdsmi
    - aomp
    - clr
    - llvm-project
    - rccl
    - rocDecode
    - rocJPEG
    - rocm-core
    - rocminfo
    - rocm_smi_lib
    - rocprofiler-register
    - rocprofiler-sdk
    - ROCR-Runtime
    - 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_systems_build_${{ job.target }}
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: ROCM_PATH
      value: $(Agent.BuildDirectory)/rocm
    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 }}
        registerROCmPackages: true
    - 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-aqlprofile.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        gpuTarget: ${{ job.target }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
    - 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:
  # build flags reference: https://rocm.docs.amd.com/projects/omnitrace/en/latest/install/install.html
        extraBuildFlags: >-
          -DROCPROFSYS_BUILD_TESTING=ON
          -DROCPROFSYS_BUILD_DYNINST=ON
          -DROCPROFSYS_BUILD_LIBUNWIND=ON
          -DROCPROFSYS_DISABLE_EXAMPLES="openmp-target"
          -DDYNINST_BUILD_TBB=ON
          -DDYNINST_BUILD_ELFUTILS=ON
          -DDYNINST_BUILD_LIBIBERTY=ON
          -DDYNINST_BUILD_BOOST=ON
          -DROCPROFSYS_USE_PAPI=ON
          -DROCPROFSYS_USE_MPI=ON
          -DCMAKE_CXX_FLAGS=-I$(Agent.BuildDirectory)/rocm/include/rocjpeg
          -DGPU_TARGETS=${{ job.target }}
          -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 }}
        registerROCmPackages: true
        extraPaths: /home/user/workspace/rocm/bin:/home/user/workspace/rocm/llvm/bin

- ${{ each job in parameters.jobMatrix.testJobs }}:
  - job: rocprofiler_systems_test_${{ job.target }}
    dependsOn: rocprofiler_systems_build_${{ job.target }}
    condition:
      and(succeeded(),
        eq(variables['ENABLE_${{ upper(job.target) }}_TESTS'], 'true'),
        not(containsValue(split(variables['DISABLED_${{ upper(job.target) }}_TESTS'], ','), variables['Build.DefinitionName'])),
        eq(${{ parameters.aggregatePipeline }}, False)
      )
    timeoutInMinutes: 180
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: ROCM_PATH
      value: $(Agent.BuildDirectory)/rocm
    pool:
      name: ${{ job.target }}_test_pool
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        registerROCmPackages: true
    - 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-aqlprofile.yml
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        gpuTarget: ${{ job.target }}
    - 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:
  # build flags reference: https://rocm.docs.amd.com/projects/omnitrace/en/latest/install/install.html
        extraBuildFlags: >-
          -DROCPROFSYS_BUILD_TESTING=ON
          -DROCPROFSYS_BUILD_DYNINST=ON
          -DROCPROFSYS_BUILD_LIBUNWIND=ON
          -DROCPROFSYS_DISABLE_EXAMPLES="openmp-target"
          -DDYNINST_BUILD_TBB=ON
          -DDYNINST_BUILD_ELFUTILS=ON
          -DDYNINST_BUILD_LIBIBERTY=ON
          -DDYNINST_BUILD_BOOST=ON
          -DROCPROFSYS_USE_PAPI=ON
          -DROCPROFSYS_USE_MPI=ON
          -DCMAKE_CXX_FLAGS=-I$(Agent.BuildDirectory)/rocm/include/rocjpeg
          -DGPU_TARGETS=${{ job.target }}
          -GNinja
    - task: Bash@3
      displayName: Set up rocprofiler-systems env
      inputs:
        targetType: inline
        script: source share/rocprofiler-systems/setup-env.sh
        workingDirectory: build
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: rocprofiler-systems
    - 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/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        pipModules: ${{ parameters.pipModules }}
        environment: test
        registerROCmPackages: true
        gpuTarget: ${{ job.target }}
        extraPaths: /home/user/workspace/rocm/bin:/home/user/workspace/rocm/llvm/bin
