parameters:
- name: componentName
  type: string
  default: aomp
- 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/aomp/blob/aomp-dev/docs/SOURCEINSTALL_PREREQUISITE.md
- name: aptPackages
  type: object
  default:
    - bison
    - ccache
    - cmake
    - flex
    - gawk
    - git
    - mesa-common-dev
    - ninja-build
    - libbabeltrace-dev
    - libbison-dev
    - libdrm-amdgpu1
    - libdrm-dev
    - libdw-dev
    - libffi-dev
    - libgmp-dev
    - liblzma-dev
    - libmpfr-dev
    - libncurses5-dev
    - libnuma-dev
    - libopenmpi-dev
    - libpci-dev
    - libssl-dev
    - libstdc++-12-dev
    - libsystemd-dev
    - libtool
    - libudev-dev
    - parallel
    - pkg-config
    - python3-dev
    - python3-pip
    - python3-setuptools
    - texinfo
- name: rocmDependencies
  type: object
  default:
    - llvm-project
    - ROCR-Runtime
- name: rocmTestDependencies
  type: object
  default:
    - clr
    - llvm-project
    - rocm-core
    - rocminfo
    - ROCR-Runtime
    - rocprofiler-register

- 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
    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
    # checkout the repos tied to openmp-extras, plus llvm-project
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: aomp-extras_repo
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: flang_repo
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: llvm-project_repo
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-vendor.yml
      parameters:
        dependencyList:
          - gtest
    - 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-cmake.yml
      parameters:
        os: ${{ job.os }}
        useAmdclang: false
        componentName: extras
        cmakeBuildDir: '$(Build.SourcesDirectory)/aomp-extras/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/aomp-extras'
        installDir: '$(Build.BinariesDirectory)/llvm'
        extraBuildFlags: >-
          -DLLVM_DIR=$(Agent.BuildDirectory)/rocm/llvm
          -DCMAKE_BUILD_TYPE=Release
          -DAOMP_STANDALONE_BUILD=0
          -DAOMP_VERSION_STRING=9.99.99
          -GNinja
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        os: ${{ job.os }}
        componentName: openmp
        cmakeBuildDir: '$(Build.SourcesDirectory)/llvm-project/openmp/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/llvm-project/openmp'
        installDir: '$(Build.BinariesDirectory)/llvm'
        extraBuildFlags: >-
          -DCMAKE_PREFIX_PATH="$(Agent.BuildDirectory)/rocm;$(Build.BinariesDirectory)"
          -DCMAKE_BUILD_TYPE=Release
          -DOPENMP_TEST_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang++
          -DOPENMP_TEST_C_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang
          -DCMAKE_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang++
          -DCMAKE_C_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang
          -DOPENMP_ENABLE_LIBOMPTARGET=1
          -DLIBOMP_COPY_EXPORTS=OFF
          -DLIBOMP_OMPD_SUPPORT=ON
          -DCMAKE_SKIP_INSTALL_RPATH=TRUE
          -DLLVM_MAIN_INCLUDE_DIR=$(Build.SourcesDirectory)/llvm-project/llvm/include
          -DLIBOMP_FORTRAN_MODULES_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/flang
          -DLIBOMP_MODULES_INSTALL_PATH=$(Build.BinariesDirectory)/llvm/include/flang/
        multithreadFlag: -- -j32
    - task: Bash@3
      displayName: 'ROCm symbolic link'
      inputs:
        targetType: inline
        script: sudo ln -s $(Agent.BuildDirectory)/rocm /opt/rocm
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        os: ${{ job.os }}
        componentName: offload
        cmakeBuildDir: '$(Build.SourcesDirectory)/llvm-project/offload/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/llvm-project/offload'
        installDir: '$(Build.BinariesDirectory)/llvm'
        extraBuildFlags: >-
          -DCMAKE_PREFIX_PATH="$(Agent.BuildDirectory)/rocm;$(Build.BinariesDirectory)"
          -DCMAKE_BUILD_TYPE=Release
          -DOPENMP_TEST_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang++
          -DOPENMP_TEST_C_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang
          -DCMAKE_CXX_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang++
          -DCMAKE_C_COMPILER=$(Agent.BuildDirectory)/rocm/llvm/bin/clang
          -DCMAKE_SKIP_INSTALL_RPATH=TRUE
          -DLLVM_MAIN_INCLUDE_DIR=$(Build.SourcesDirectory)/llvm-project/llvm/include
          -DLIBOMPTARGET_LLVM_INCLUDE_DIRS=$(Build.SourcesDirectory)/llvm-project/llvm/include
          -DCMAKE_EXE_LINKER_FLAGS="-L$(Agent.BuildDirectory)/rocm/llvm/lib"
          -DCMAKE_SHARED_LINKER_FLAGS="-L$(Agent.BuildDirectory)/rocm/llvm/lib"
          -GNinja
    - 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
    - ${{ if eq(job.os, 'ubuntu2204') }}:
      - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
        parameters:
          aptPackages: ${{ parameters.aptPackages }}

- ${{ 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
    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/local-artifact-download.yml
      parameters:
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/dependencies-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmTestDependencies }}
        os: ${{ job.os }}
    - task: Bash@3
      displayName: ROCm symbolic link
      inputs:
        targetType: inline
        script: sudo ln -s $(Agent.BuildDirectory)/rocm /opt/rocm
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: ${{ parameters.checkoutRepo }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/checkout.yml
      parameters:
        checkoutRepo: aomp-extras_repo
    # these copy steps are from the aomp prototype script for test prep
    - task: CopyFiles@2
      displayName: 'Copy AOMP contents'
      inputs:
        CleanTargetFolder: false
        SourceFolder: $(Build.SourcesDirectory)/aomp
        Contents: |
          **
          !**/.git/**
          !**/.github/**
          !**/.gitignore
        TargetFolder: $(Agent.BuildDirectory)/rocm/share/openmp-extras/tests
        retryCount: 3
    - task: CopyFiles@2
      displayName: 'Copy FileCheck'
      inputs:
        CleanTargetFolder: false
        SourceFolder: $(Agent.BuildDirectory)/rocm/llvm/bin
        Contents: FileCheck
        TargetFolder: $(Agent.BuildDirectory)/rocm/share/openmp-extras/tests/bin
        retryCount: 3
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/gpu-diagnostics.yml
    - task: Bash@3
      displayName: Test AOMP
      continueOnError: true
      inputs:
        targetType: inline
        script: ./run_rocm_test.sh
        workingDirectory: $(Build.SourcesDirectory)/aomp/bin
      env:
        AOMP: $(Agent.BuildDirectory)/rocm/llvm
        AOMP_REPOS_TEST: $(Build.SourcesDirectory)/aomp-test
        AOMP_TEST_DIR: $(Build.SourcesDirectory)/aomp-test
        SKIP_TEST_PACKAGE: 1
        MAINLINE_BUILD: 1
        SUITE_LIST: smoke
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/docker-container.yml
      parameters:
        aptPackages: ${{ parameters.aptPackages }}
        environment: test
        gpuTarget: ${{ job.target }}
        optSymLink: true
