parameters:
- name: componentName
  type: string
  default: ROCgdb
- 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
# reference: https://github.com/ROCm/ROCgdb/blob/amd-staging/README-ROCM.md
- name: aptPackages
  type: object
  default:
    - bison
    - cmake
    - dejagnu
    - flex
    - libbabeltrace-dev
    - libexpat-dev
    - libgmp-dev
    - liblzma-dev
    - libmpfr-dev
    - ncurses-dev
    - pkg-config
    - python3-dev
    - python3-pip
    - texinfo
    - zlib1g-dev
- name: rocmDependencies
  type: object
  default:
    - clr
    - llvm-project
    - ROCdbgapi
    - rocminfo
    - rocprofiler-register
    - ROCR-Runtime

- name: jobMatrix
  type: object
  default:
    buildJobs:
      - { os: ubuntu2204, packageManager: apt }
      - { os: almalinux8, packageManager: dnf }
    testJobs:
      - { os: ubuntu2204, packageManager: apt, target: gfx942 }
      - { os: ubuntu2204, packageManager: apt, target: gfx90a }

jobs:
- ${{ each job in parameters.jobMatrix.buildJobs }}:
  - job: ${{ parameters.componentName }}_build_${{ job.os }}
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: PKG_CONFIG_PATH
      value: $(Agent.BuildDirectory)/rocm/share/pkgconfig
    pool:
      ${{ if eq(job.os, 'ubuntu2404') }}:
        name: rocm-ci_medium_build_pool_2404
      ${{ else }}:
        name: ${{ variables.MEDIUM_BUILD_POOL }}
    ${{ if eq(job.os, 'almalinux8') }}:
      container:
        image: rocmexternalcicd.azurecr.io/manylinux228:latest
        endpoint: ContainerService3
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        packageManager: ${{ job.packageManager }}
    - 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
      parameters:
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        os: ${{ job.os }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-autotools.yml
      parameters:
        os: ${{ job.os }}
        configureFlags: >-
          --program-prefix=roc
          --enable-64-bit-bfd
          --enable-targets="x86_64-linux-gnu,amdgcn-amd-amdhsa"
          --disable-ld
          --disable-gas
          --disable-gdbserver
          --disable-sim
          --enable-tui
          --disable-gdbtk
          --disable-shared
          --disable-gprofng
          --with-expat
          --with-system-zlib
          --without-guile
          --with-babeltrace
          --with-lzma
          --with-python=python3
          --with-rocm-dbgapi=$(Agent.BuildDirectory)/rocm
          LDFLAGS="-Wl,--enable-new-dtags,-rpath=$(Agent.BuildDirectory)/rocm/lib"
        makeCallPrefix: LD_RUN_PATH='${ORIGIN}/../lib'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        componentName: ${{ parameters.componentName }}
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-links.yml

- ${{ each job in parameters.jobMatrix.testJobs }}:
  - job: ${{ parameters.componentName }}_test_${{ job.os }}_${{ job.target }}
    dependsOn: ${{ parameters.componentName }}_build_${{ job.os }}
    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)
      )
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: PKG_CONFIG_PATH
      value: $(Agent.BuildDirectory)/rocm/share/pkgconfig
    pool: ${{ job.target }}_test_pool
    workspace:
      clean: all
    steps:
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-other.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        packageManager: ${{ job.packageManager }}
    - 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
      parameters:
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        os: ${{ job.os }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-autotools.yml
      parameters:
        os: ${{ job.os }}
        configureFlags: >-
          --program-prefix=roc
          --enable-64-bit-bfd
          --enable-targets="x86_64-linux-gnu,amdgcn-amd-amdhsa"
          --disable-ld
          --disable-gas
          --disable-gdbserver
          --disable-sim
          --enable-tui
          --disable-gdbtk
          --disable-shared
          --disable-gprofng
          --with-expat
          --with-system-zlib
          --without-guile
          --with-babeltrace
          --with-lzma
          --with-python=python3
          --with-rocm-dbgapi=$(Agent.BuildDirectory)/rocm
          LDFLAGS="-Wl,--enable-new-dtags,-rpath=$(Agent.BuildDirectory)/rocm/lib"
        makeCallPrefix: LD_RUN_PATH='${ORIGIN}/../lib'
    - task: Bash@3
      displayName: Setup test environment
      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
          echo "##vso[task.prependpath]/opt/rocm/bin"
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/gpu-diagnostics.yml
    - task: Bash@3
      displayName: check-gdb
      continueOnError: true
      inputs:
        targetType: inline
        script: |
          ${{ iif(eq(job.os, 'almalinux8'), 'source /opt/rh/gcc-toolset-14/enable', '') }}
          make check-gdb TESTS=gdb.rocm/simple.exp
        workingDirectory: $(Build.SourcesDirectory)
    - task: Bash@3
      displayName: print gdb log
      inputs:
        targetType: inline
        script: find -name gdb.log -exec cat {} \;
        workingDirectory: $(Build.SourcesDirectory)
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        environment: combined
        gpuTarget: ${{ job.target }}
        extraEnvVars:
          - PKG_CONFIG_PATH:::/home/user/workspace/rocm/share/pkgconfig
