parameters:
  arch:
  demands: []

jobs:
  - job: Build_${{ parameters.arch }}
    displayName: Build
    variables:
      ${{ if eq(variables['Build.Reason'], 'ResourceTrigger') }}:
        POSTFIX: ucx-${{ replace(variables['Build.SourceBranch'], 'refs/heads/', '') }}
      ${{ if eq(variables['Build.Reason'], 'IndividualCI') }}:
        POSTFIX: ucx-${{ replace(variables['Build.SourceBranch'], 'refs/tags/v', '') }}
      ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
        POSTFIX: ucx-pr$(System.PullRequest.PullRequestNumber)

    strategy:
      matrix:
        # common for both x86 and ARM
        centos8_cuda11_${{ parameters.arch }}:
          build_container: centos8_cuda11_${{ parameters.arch }}
          artifact_name: $(POSTFIX)-centos8-mofed5-cuda11-${{ parameters.arch }}.tar.bz2
        ubuntu18_cuda11_${{ parameters.arch }}:
          build_container: ubuntu18_cuda11_${{ parameters.arch }}
          artifact_name: $(POSTFIX)-ubuntu18.04-mofed5-cuda11-${{ parameters.arch }}.tar.bz2
        ubuntu20_cuda11_${{ parameters.arch }}:
          build_container: ubuntu20_cuda11_${{ parameters.arch }}
          artifact_name: $(POSTFIX)-ubuntu20.04-mofed5-cuda11-${{ parameters.arch }}.tar.bz2
        ubuntu22_cuda11_${{ parameters.arch }}:
          build_container: ubuntu22_cuda11_${{ parameters.arch }}
          artifact_name: $(POSTFIX)-ubuntu22.04-mofed5-cuda11-${{ parameters.arch }}.tar.bz2
        ubuntu20_cuda12_${{ parameters.arch }}:
          build_container: ubuntu20_cuda12_${{ parameters.arch }}
          artifact_name: $(POSTFIX)-ubuntu20.04-mofed5-cuda12-${{ parameters.arch }}.tar.bz2
        ubuntu22_cuda12_${{ parameters.arch }}:
          build_container: ubuntu22_cuda12_${{ parameters.arch }}
          artifact_name: $(POSTFIX)-ubuntu22.04-mofed5-cuda12-${{ parameters.arch }}.tar.bz2
        # x86 only
        ${{ if eq(parameters.arch, 'x86_64') }}:
          centos7_cuda11_${{ parameters.arch }}:
            build_container: centos7_cuda11_${{ parameters.arch }}
            artifact_name: $(POSTFIX)-centos7-mofed5-cuda11-${{ parameters.arch }}.tar.bz2
          centos7_cuda12_${{ parameters.arch }}:
            build_container: centos7_cuda12_${{ parameters.arch }}
            artifact_name: $(POSTFIX)-centos7-mofed5-cuda12-${{ parameters.arch }}.tar.bz2
          ubuntu16_cuda11_${{ parameters.arch }}:
            build_container: ubuntu16_cuda11_${{ parameters.arch }}
            artifact_name: $(POSTFIX)-ubuntu16.04-mofed5-cuda11-${{ parameters.arch }}.tar.bz2
          ubuntu18_cuda12_${{ parameters.arch }}:
            build_container: ubuntu18_cuda12_${{ parameters.arch }}
            artifact_name: $(POSTFIX)-ubuntu18.04-mofed5-cuda12-${{ parameters.arch }}.tar.bz2

    container: $[ variables['build_container'] ]
    pool:
      name: MLNX
      demands: ${{ parameters.demands }}


    steps:
      - checkout: self
        clean: true
        fetchDepth: 100
        path: "we/need/to/go/deeper"
        # ^ Avoid rpmbuild error: Dest dir longer than base dir is not supported

      - bash: |
          set -eEx
          ./autogen.sh
          mkdir -p pkg-build
          cd pkg-build
          ../contrib/configure-release --with-cuda --with-java=no
          ../contrib/buildrpm.sh -s -t -b --noclean
          cd rpm-dist/$(uname -m)
          tar -cjf "../../../${AZ_ARTIFACT_NAME}" *.rpm
          cd ../../..
          tar -tjf "${AZ_ARTIFACT_NAME}"
        displayName: Build RPM package
        condition: and(succeeded(), contains(variables['artifact_name'], 'centos'))
        env:
          AZ_ARTIFACT_NAME: $(artifact_name)

      - bash: |
          set -eEx

          # Build
          ./autogen.sh
          ./contrib/configure-release --with-cuda --with-java=no
          make dist
          tarball=$(echo ucx*.tar.gz)
          tar -xzvf ${tarball}              # extract the sources in a subdirectory
          cd $(tar tf ${tarball} | head -1) # go to extracted tarball directory
          echo 10 > debian/compat   # https://www.debian.org/doc/manuals/maint-guide/dother.en.htmdpl#compat
          dpkg-buildpackage -us -uc -Pcuda,xpmem
          cd ..                             # Move back to the working directory

          # Rename DEB files          
          VER="${POSTFIX#ucx-}"     # Remove 'ucx-' prefix from the POSTFIX string
          find . -name "ucx*.deb" -exec bash -c 'mv "$1" "${1%%_*}-'"${VER}"'.deb"' _ {} \;
          find . -name '*.deb'      # Show new names

          # Remove superfluous dependency
          dpkg-deb -R "ucx-cuda-${VER}.deb" tmp    # Extract
          sed -i 's/libnvidia-compute-[0-9]* | libnvidia-ml1, //g' tmp/DEBIAN/control
          dpkg-deb -b tmp "ucx-cuda-${VER}.deb"        # Rebuild
          dpkg-deb -I "ucx-cuda-${VER}.deb"
          dpkg-deb -I "ucx-${VER}.deb"

          # Package
          tar -cjf "${AZ_ARTIFACT_NAME}" *.deb  # Package all DEBs
          tar -tjf "${AZ_ARTIFACT_NAME}"
        displayName: Build DEB package
        condition: and(succeeded(), contains(variables['artifact_name'], 'ubuntu'))
        env:
          AZ_ARTIFACT_NAME: $(artifact_name)

      - task: GithubRelease@0
        condition: eq(variables['Build.Reason'], 'IndividualCI')
        displayName: Upload artifacts
        inputs:
          githubConnection: release
          repositoryName: openucx/ucx
          action: edit
          tag: $(Build.SourceBranchName)
          isDraft: true
          addChangeLog: false
          assetUploadMode: replace
          assets: "./$(artifact_name)"
