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:
    - cmake
    - libnuma-dev
    - ninja-build
    - pkg-config
    - python-is-python3
    - python3-pip
    - zlib1g-dev
- name: rocmDependencies
  type: object
  default:
    - rocm-cmake

- name: jobMatrix
  type: object
  default:
    buildJobs:
      - { os: ubuntu2204, packageManager: apt }
      # - { os: ubuntu2404, packageManager: apt }
      - { os: almalinux8, packageManager: dnf }

jobs:
- ${{ each job in parameters.jobMatrix.buildJobs }}:
  - job: llvm_project_${{ job.os }}
    pool:
      ${{ if eq(job.os, 'ubuntu2404') }}:
        name: 'rocm-ci_high_build_pool_2404' #temporarily using 'high' pool while 'ultra' is down
      ${{ else }}:
        name: 'rocm-ci_ultra_build_pool'
    ${{ if eq(job.os, 'almalinux8') }}:
      container:
        image: rocmexternalcicd.azurecr.io/manylinux228:latest
        endpoint: ContainerService3
    variables:
    - group: common
    - template: /.azuredevops/variables-global.yml
    - name: HIP_DEVICE_LIB_PATH
      value: '$(Build.BinariesDirectory)/amdgcn/bitcode'
    - name: HIP_PATH
      value: '$(Agent.BuildDirectory)/rocm'
    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-rocm.yml
      parameters:
        checkoutRef: ${{ parameters.checkoutRef }}
        dependencyList: ${{ parameters.rocmDependencies }}
        aggregatePipeline: ${{ parameters.aggregatePipeline }}
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        componentName: rocm-llvm
        os: ${{ job.os }}
        useAmdclang: false
        extraBuildFlags: >-
          -DCMAKE_PREFIX_PATH="$(Build.BinariesDirectory)/llvm;$(Build.BinariesDirectory)"
          -DCMAKE_BUILD_TYPE=Release
          -DLLVM_ENABLE_PROJECTS=clang;lld;clang-tools-extra;mlir;flang
          -DLLVM_ENABLE_RUNTIMES=compiler-rt;libunwind;libcxx;libcxxabi
          -DCLANG_ENABLE_AMDCLANG=ON
          -DLLVM_TARGETS_TO_BUILD=AMDGPU;X86
          -DLIBCXX_ENABLE_SHARED=OFF
          -DLIBCXX_ENABLE_STATIC=ON
          -DLIBCXX_INSTALL_LIBRARY=OFF
          -DLIBCXX_INSTALL_HEADERS=OFF
          -DLIBCXXABI_ENABLE_SHARED=OFF
          -DLIBCXXABI_ENABLE_STATIC=ON
          -DLIBCXXABI_INSTALL_STATIC_LIBRARY=OFF
          -DLLVM_BUILD_DOCS=OFF
          -DLLVM_ENABLE_SPHINX=OFF
          -DLLVM_ENABLE_ASSERTIONS=OFF
          -DLLVM_ENABLE_Z3_SOLVER=OFF
          -DLLVM_ENABLE_ZLIB=ON
          -DCLANG_DEFAULT_LINKER=lld
          -DCLANG_DEFAULT_RTLIB=compiler-rt
          -DCLANG_DEFAULT_UNWINDLIB=libgcc
          -DSANITIZER_AMDGPU=OFF
          -DPACKAGE_VENDOR=AMD
          -DCLANG_LINK_FLANG_LEGACY=ON
          -DCMAKE_CXX_STANDARD=17
          -DROCM_LLVM_BACKWARD_COMPAT_LINK=$(Build.BinariesDirectory)/llvm
          -DROCM_LLVM_BACKWARD_COMPAT_LINK_TARGET=./lib/llvm
          -GNinja
        cmakeBuildDir: '$(Build.SourcesDirectory)/llvm/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/llvm'
        installDir: '$(Build.BinariesDirectory)/llvm'
    # use llvm-lit to run unit tests for llvm, clang, and lld
    - task: Bash@3
      displayName: 'Copy llvm-lit to install directory'
      inputs:
        targetType: inline
        script: |
          cp $(Build.SourcesDirectory)/llvm/build/bin/llvm-lit $(Build.BinariesDirectory)/llvm/bin/
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: check-llvm
        testDir: 'llvm/build'
        testExecutable: './bin/llvm-lit'
        testParameters: '-q --xunit-xml-output=llvm_test_output.xml --filter-out="live-debug-values-spill-tracking" ./test'
        testOutputFile: llvm_test_output.xml
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: check-clang
        testDir: 'llvm/build'
        testExecutable: './bin/llvm-lit'
        testParameters: '-q --xunit-xml-output=clang_test_output.xml ./tools/clang/test'
        testOutputFile: clang_test_output.xml
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: check-lld
        testDir: 'llvm/build'
        testExecutable: './bin/llvm-lit'
        testParameters: '-q --xunit-xml-output=lld_test_output.xml ./tools/lld/test'
        testOutputFile: lld_test_output.xml
        os: ${{ job.os }}
    - task: CopyFiles@2
      displayName: Copy FileCheck for Publishing
      inputs:
        CleanTargetFolder: false
        SourceFolder: llvm/build/bin
        Contents: FileCheck
        TargetFolder: $(Build.BinariesDirectory)/llvm/bin
        retryCount: 3
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        componentName: device-libs
        os: ${{ job.os }}
        useAmdclang: false
        extraBuildFlags: >-
          -DCMAKE_PREFIX_PATH="$(Build.SourcesDirectory)/llvm/build"
          -DCMAKE_BUILD_TYPE=Release
        cmakeBuildDir: '$(Build.SourcesDirectory)/amd/device-libs/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/amd/device-libs'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        componentName: comgr
        os: ${{ job.os }}
        useAmdclang: false
        extraBuildFlags: >-
          -DCMAKE_PREFIX_PATH="$(Build.SourcesDirectory)/llvm/build;$(Build.SourcesDirectory)/amd/device-libs/build"
          -DCOMGR_DISABLE_SPIRV=1
          -DCMAKE_BUILD_TYPE=Release
        cmakeBuildDir: '$(Build.SourcesDirectory)/amd/comgr/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/amd/comgr'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/test.yml
      parameters:
        componentName: comgr
        testParameters: '--output-on-failure --force-new-ctest-process --output-junit comgr_test_output.xml'
        testDir: 'amd/comgr/build'
        testOutputFile: comgr_test_output.xml
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/build-cmake.yml
      parameters:
        componentName: hipcc
        os: ${{ job.os }}
        useAmdclang: false
        extraBuildFlags: >-
          -DCMAKE_BUILD_TYPE=Release
          -DHIPCC_BACKWARD_COMPATIBILITY=OFF
        cmakeBuildDir: '$(Build.SourcesDirectory)/amd/hipcc/build'
        cmakeSourceDir: '$(Build.SourcesDirectory)/amd/hipcc'
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/manifest.yml
      parameters:
        os: ${{ job.os }}
    - template: ${{ variables.CI_TEMPLATE_PATH }}/steps/artifact-upload.yml
      parameters:
        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 }}
          environment: combined
          extraEnvVars:
            - HIP_DEVICE_LIB_PATH:::/home/user/workspace/bin/amdgcn/bitcode
            - HIP_PATH:::/home/user/workspace/rocm
