# See https://aka.ms/yaml

trigger:
  - master
  - stable-v*
  - dev/stable-v*/*
pr:
  - master

resources:
  containers:
    - container: azp
      image: ucfconsort.azurecr.io/rdma-core/azure_pipelines:44.0
      endpoint: ucfconsort_registry
    - container: centos7
      image: ucfconsort.azurecr.io/rdma-core/centos7:25.0
      endpoint: ucfconsort_registry
    - container: centos8
      image: ucfconsort.azurecr.io/rdma-core/centos8:44.0
      endpoint: ucfconsort_registry
    - container: centos9
      image: ucfconsort.azurecr.io/rdma-core/centos9:44.0
      endpoint: ucfconsort_registry
    - container: fedora
      image: ucfconsort.azurecr.io/rdma-core/fc44:63.0
      endpoint: ucfconsort_registry
    - container: xenial
      image: ucfconsort.azurecr.io/rdma-core/ubuntu-16.04:28.0
      endpoint: ucfconsort_registry
    - container: bionic
      image: ucfconsort.azurecr.io/rdma-core/ubuntu-18.04:29.0
      endpoint: ucfconsort_registry
    - container: focal
      image: ucfconsort.azurecr.io/rdma-core/ubuntu-20.04:44.0
      endpoint: ucfconsort_registry
    - container: leap
      image: ucfconsort.azurecr.io/rdma-core/opensuse-15.0:25.0
      endpoint: ucfconsort_registry
    - container: i386
      image: ucfconsort.azurecr.io/rdma-core/debian-11-i386:37.0
      options: --platform linux/386
      endpoint: ucfconsort_registry

stages:
  - stage: Build
    jobs:
      - job: Compile
        displayName: Compile Tests
        pool:
          vmImage: 'ubuntu-latest'
        container: azp
        steps:
          - task: PythonScript@0
            displayName: checkpatch
            condition: eq(variables['Build.Reason'], 'PullRequest')
            inputs:
              scriptPath: buildlib/azp-checkpatch
              pythonInterpreter: /usr/bin/python3

          - bash: |
              set -e
              mkdir build-gcc12
              cd build-gcc12
              CC=gcc-12 cmake -GNinja .. -DIOCTL_MODE=both -DENABLE_STATIC=1 -DENABLE_WERROR=1
              ninja
            displayName: gcc 12.1 Compile

          - task: PythonScript@0
            displayName: Check Build Script
            inputs:
              scriptPath: buildlib/check-build
              arguments: --src .. --cc gcc-12
              workingDirectory: build-gcc12
              pythonInterpreter: /usr/bin/python3

          # Run sparse on the subdirectories which are sparse clean
          - bash: |
              set -e
              mkdir build-sparse
              mv CMakeLists.txt CMakeLists-orig.txt
              grep -v "# NO SPARSE" CMakeLists-orig.txt > CMakeLists.txt
              cd build-sparse
              CC=cgcc cmake -GNinja .. -DIOCTL_MODE=both -DNO_PYVERBS=1 -DENABLE_WERROR=1
              ninja | grep -v '^\[' | tee out
              # sparse does not fail gcc on messages
              if [ -s out ]; then
                 false
              fi
              mv ../CMakeLists-orig.txt ../CMakeLists.txt
            displayName: sparse Analysis

          - bash: |
              set -e
              mkdir build-clang
              cd build-clang
              CC=clang-15 cmake -GNinja .. -DCMAKE_BUILD_TYPE=Debug -DIOCTL_MODE=both -DENABLE_WERROR=1
              ninja
            displayName: clang 15 Compile

          - bash: |
              set -e
              mv util/udma_barrier.h util/udma_barrier.h.old
              echo "#error Fail" >> util/udma_barrier.h
              cd build-gcc12
              rm CMakeCache.txt
              CC=gcc-12 cmake -GNinja .. -DIOCTL_MODE=both -DENABLE_WERROR=1
              ninja
              mv ../util/udma_barrier.h.old ../util/udma_barrier.h
            displayName: Simulate non-coherent DMA Platform Compile

          - bash: |
              set -e
              mkdir build-arm64
              cd build-arm64
              CC=aarch64-linux-gnu-gcc-12 PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig/ cmake -GNinja .. -DIOCTL_MODE=both -DNO_PYVERBS=1 -DENABLE_WERROR=1
              ninja
            displayName: gcc 12.1 ARM64 Compile

          - bash: |
              set -e
              mkdir build-ppc64el
              cd build-ppc64el
              CC=powerpc64le-linux-gnu-gcc-12 PKG_CONFIG_PATH=/usr/lib/powerpc64le-linux-gnu/pkgconfig/ cmake -GNinja .. -DIOCTL_MODE=both -DNO_PYVERBS=1 -DENABLE_WERROR=1
              ninja
            displayName: gcc 12.1 PPC64EL Compile

      - job: Compile32
        displayName: Compile Tests 32 bit
        pool:
          vmImage: 'ubuntu-latest'
        container: i386
        steps:
          - bash: |
              set -e
              mkdir build-i386
              cd build-i386
              cmake -GNinja .. -DIOCTL_MODE=both -DENABLE_WERROR=1
              ninja
            displayName: gcc 10.2 i386 Compile

      - job: SrcPrep
        displayName: Build Source Tar
        pool:
          vmImage: 'ubuntu-latest'
        container: azp
        steps:
          - checkout: self
            fetchDepth: 1

          - bash: |
              set -e
              mkdir build-pandoc artifacts
              cd build-pandoc
              CC=gcc-12 cmake -GNinja ..
              ninja docs
              cd ../artifacts
              # FIXME: Check Build.SourceBranch for tag consistency
              python3 ../buildlib/cbuild make-dist-tar ../build-pandoc
            displayName: Prebuild Documentation

          - task: PublishPipelineArtifact@0
            inputs:
              # Contains an rdma-core-XX.tar.gz file
              artifactName: source_tar
              targetPath: artifacts

      - job: RPM_Distros
        displayName: Test Build RPMs for
        dependsOn: SrcPrep
        pool:
          vmImage: 'ubuntu-latest'
        strategy:
          matrix:
            centos7:
              CONTAINER: centos7
              SPEC: redhat/rdma-core.spec
              RPMBUILD_OPTS:  --define 'EXTRA_CMAKE_FLAGS -DCMAKE_BUILD_TYPE=Debug -DENABLE_WERROR=1'
            centos8:
              CONTAINER: centos8
              SPEC: redhat/rdma-core.spec
              RPMBUILD_OPTS:  --define 'EXTRA_CMAKE_FLAGS -DCMAKE_BUILD_TYPE=Debug -DENABLE_WERROR=1'
            centos9:
              CONTAINER: centos9
              SPEC: redhat/rdma-core.spec
              RPMBUILD_OPTS:  --define 'EXTRA_CMAKE_FLAGS -DCMAKE_BUILD_TYPE=Debug -DENABLE_WERROR=1'
            fedora44:
              CONTAINER: fedora
              SPEC: redhat/rdma-core.spec
              RPMBUILD_OPTS:  --define 'EXTRA_CMAKE_FLAGS -DCMAKE_BUILD_TYPE=Debug -DENABLE_WERROR=1'
            leap:
              CONTAINER: leap
              SPEC: suse/rdma-core.spec
              RPMBUILD_OPTS:  --define 'EXTRA_CMAKE_FLAGS -DCMAKE_BUILD_TYPE=Debug -DENABLE_WERROR=1' --without=curlmini
        container: $[ variables['CONTAINER'] ]
        steps:
          - checkout: none

          - task: DownloadPipelineArtifact@2
            inputs:
              artifactName: source_tar
              targetPath: .

          - bash: |
              set -e
              mkdir SOURCES tmp
              tar --wildcards -xzf rdma-core*.tar.gz  */$(SPEC) --strip-components=2
              RPM_SRC=$((rpmspec -P *.spec || grep ^Source: *.spec) | awk '/^Source:/{split($0,a,"[ \t]+");print(a[2])}')
              (cd SOURCES && ln -sf ../rdma-core*.tar.gz "$RPM_SRC")
              rpmbuild --define '_tmppath '$(pwd)'/tmp' --define '_topdir '$(pwd) -bb *.spec $(RPMBUILD_OPTS)
            displayName: Perform Package Build

      - job: DEB_Distros
        displayName: Test Build DEBs for
        dependsOn: SrcPrep
        pool:
          vmImage: 'ubuntu-latest'
        strategy:
          matrix:
            xenial:
              CONTAINER: xenial
            bionic:
              CONTAINER: bionic
            focal:
              CONTAINER: focal
            jammy:
              CONTAINER: azp
              LINTIAN: true
        container: $[ variables['CONTAINER'] ]
        steps:
          - checkout: none

          - task: DownloadPipelineArtifact@2
            inputs:
              artifactName: source_tar
              targetPath: .

          - bash: |
              set -e
              mv *.tar.gz src.tar.gz
              tar -xzf src.tar.gz
              cd rdma-core*/
              dpkg-buildpackage -b -d
            displayName: Perform Package Build

          - bash: |
              lintian *.deb
            displayName: Debian Lintian for .deb packages
            condition: eq(variables['LINTIAN'], 'true')
