parameters:
- name: os
  type: string
  default: 'ubuntu2204'
- name: componentName
  type: string
  default: ''
- name: extraBuildFlags
  type: string
  default: ''
- name: extraCxxFlags
  type: string
  default: ''
- name: multithreadFlag
  type: string
  default: ''
- name: cmakeBuildDir
  type: string
  default: $(Agent.BuildDirectory)/s/build
- name: cmakeSourceDir
  type: string
  default: $(Agent.BuildDirectory)/s
- name: customBuildTarget
  type: string
  default: ''
- name: cmakeTarget
  type: string
  default: 'install'
- name: cmakeTargetDir
  type: string
  default: '.'
- name: installDir
  type: string
  default: '$(Build.BinariesDirectory)'
- name: customInstallPath
  type: boolean
  default: true
- name: installEnabled
  type: boolean
  default: true
# for jobs that rebuild during install step and use ninja
# set to true to save time, only applies for almalinux8
- name: consolidateBuildAndInstall
  type: boolean
  default: false
- name: printDiskSpace
  type: boolean
  default: true
# todo: make this control cxx and c compiler flags
- name: useAmdclang
  type: boolean
  default: true

# for cmake calls, set env variables for AlmaLinux 8
# to simulate running source /opt/rh/gcc-toolset-14/enable for the session

steps:
# create workingDirectory if it does not exist and change into it
# call cmake from within that directory using $cmakeArgs as its parameters
- task: CMake@1
  displayName: '${{parameters.componentName }} CMake Flags'
  ${{ if eq(parameters.os, 'almalinux8')}}:
    env:
      PATH: "/opt/rh/gcc-toolset-14/root/usr/bin:$(PATH)"
      MANPATH: "/opt/rh/gcc-toolset-14/root/usr/share/man:$(MANPATH)"
      INFOPATH: "/opt/rh/gcc-toolset-14/root/usr/share/info:$(INFOPATH)"
      PCP_DIR: "/opt/rh/gcc-toolset-14/root"
      LD_LIBRARY_PATH: "/opt/rh/gcc-toolset-14/root/usr/lib64:/opt/rh/gcc-toolset-14/root/usr/lib:$(LD_LIBRARY_PATH)"
      PKG_CONFIG_PATH: "/opt/rh/gcc-toolset-14/root/usr/lib64/pkgconfig:$(PKG_CONFIG_PATH)"
  inputs:
    workingDirectory: ${{ parameters.cmakeBuildDir }}
    cmakeArgs: >-
      ${{ iif(parameters.customInstallPath, join('', format('-DCMAKE_INSTALL_PREFIX={0}', parameters.installDir)), '') }}
      ${{ iif(eq(parameters.os, 'almalinux8'), '-DCMAKE_SHARED_LINKER_FLAGS="-L$(Agent.BuildDirectory)/rocm/lib64 -L/opt/rh/gcc-toolset-14/root/usr/lib/gcc/x86_64-redhat-linux/14/"', '') }}
      ${{ iif(eq(parameters.os, 'almalinux8'), '-DCMAKE_EXE_LINKER_FLAGS="-L$(Agent.BuildDirectory)/rocm/lib64 -L/opt/rh/gcc-toolset-14/root/usr/lib/gcc/x86_64-redhat-linux/14/"', '') }}
      -DCMAKE_CXX_FLAGS="${{ parameters.extraCxxFlags }} ${{ iif(and(eq(parameters.os, 'almalinux8'), parameters.useAmdclang), '--gcc-toolchain=/opt/rh/gcc-toolset-14/root', '') }}"
      ${{ parameters.extraBuildFlags }}
      ${{ parameters.cmakeSourceDir }}
- ${{ if parameters.printDiskSpace }}:
  - script: df -h
    displayName: Disk space before build
# equivalent to running make $cmakeTargetDir from $cmakeBuildDir
# i.e., cd $cmakeBuildDir; make $cmakeTargetDir
- task: CMake@1
  ${{ if and( eq(parameters.os, 'almalinux8'), eq(parameters.consolidateBuildAndInstall , true)) }}:
    displayName: '${{ parameters.componentName }} CMake Build and Install'
  ${{ else }}:
    displayName: '${{ parameters.componentName }} CMake Build'
  ${{ if eq(parameters.os, 'almalinux8')}}:
    env:
      PATH: "/opt/rh/gcc-toolset-14/root/usr/bin:$(PATH)"
      MANPATH: "/opt/rh/gcc-toolset-14/root/usr/share/man:$(MANPATH)"
      INFOPATH: "/opt/rh/gcc-toolset-14/root/usr/share/info:$(INFOPATH)"
      PCP_DIR: "/opt/rh/gcc-toolset-14/root"
      LD_LIBRARY_PATH: "/opt/rh/gcc-toolset-14/root/usr/lib64:/opt/rh/gcc-toolset-14/root/usr/lib:$(LD_LIBRARY_PATH)"
      PKG_CONFIG_PATH: "/opt/rh/gcc-toolset-14/root/usr/lib64/pkgconfig:$(PKG_CONFIG_PATH)"
  inputs:
    workingDirectory: ${{ parameters.cmakeBuildDir }}
    ${{ if eq(parameters.os, 'almalinux8') }}:
      cmakeArgs: >-
        --build ${{ parameters.cmakeTargetDir }}
        ${{ iif(and(eq(parameters.consolidateBuildAndInstall, true), ne(parameters.cmakeTarget, '')), format('--target {0}', parameters.cmakeTarget), '') }}
        ${{ iif(and(ne(parameters.customBuildTarget, ''), ne(parameters.consolidateBuildAndInstall, true)), format('--target {0}', parameters.customBuildTarget), '') }}
        ${{ parameters.multithreadFlag }}
    ${{ if ne(parameters.os, 'almalinux8') }}:
      cmakeArgs: >-
        --build ${{ parameters.cmakeTargetDir }}
        ${{ iif(ne(parameters.customBuildTarget, ''), format('--target {0}', parameters.customBuildTarget), '') }}
        ${{ parameters.multithreadFlag }}
- ${{ if parameters.printDiskSpace }}:
  - script: df -h
    displayName: Disk space after build
# equivalent to running make $cmakeTarget from $cmakeBuildDir
# e.g., make install
- ${{ if and(eq(parameters.installEnabled, true), or(ne(parameters.os, 'almalinux8'), eq(parameters.consolidateBuildAndInstall, false))) }}:
  - task: CMake@1
    displayName: '${{parameters.componentName }} ${{ parameters.cmakeTarget }}'
    inputs:
      workingDirectory: ${{ parameters.cmakeBuildDir }}
      cmakeArgs: '--build ${{ parameters.cmakeTargetDir }} --target ${{ parameters.cmakeTarget }}'
