def hipBuildTest(String backendLabel) {
        node(backendLabel) {
          stage("SYNC - ${backendLabel}") {

            // Checkout hip repository with the PR patch
            dir("${WORKSPACE}/hip") {
               checkout scm
               env.HIP_DIR = "${WORKSPACE}" + "/hip"
            }

            // Clone hip-tests repository
            dir("${WORKSPACE}/hip-tests") {
              git branch: 'develop',
              url: 'https://github.com/ROCm-Developer-Tools/hip-tests'
              env.HIP_TESTS_DIR = "${WORKSPACE}" + "/hip-tests"
            }

            // Clone clr repository
            dir("${WORKSPACE}/clr") {
              git branch: 'develop',
              credentialsId: 'branch-credentials',
              url: 'https://github.com/ROCm-Developer-Tools/clr'
              env.CLR_DIR = "${WORKSPACE}" + "/clr"
            }

            // Clone hipcc repspoitory
            dir("${WORKSPACE}/hipcc") {
              git branch: 'develop',
              credentialsId: 'branch-credentials',
              url: 'https://github.com/ROCm-Developer-Tools/hipcc'
              env.HIPCC_DIR = "${WORKSPACE}" + "/hipcc"
            }
        }

        stage("BUILD HIP - ${backendLabel}") {
            // Running the build on clr workspace
            dir("${WORKSPACE}/clr") {
                sh  """#!/usr/bin/env bash
                    set -x
                    rm -rf build
                    mkdir -p build
                    cd build
                    # Check if backend label contains string "amd" or backend host is a server with amd gpu
                    if [[ $backendLabel =~ amd ]]; then
                        cmake -DCLR_BUILD_HIP=ON -DHIP_PATH=\$PWD/install -DHIPCC_BIN_DIR=\$HIPCC_DIR/bin -DHIP_COMMON_DIR=\$HIP_DIR -DCMAKE_PREFIX_PATH="/opt/rocm/" -DCMAKE_INSTALL_PREFIX=\$PWD/install ..
                    else
                        cmake -DCLR_BUILD_HIP=ON -DHIP_PLATFORM=nvidia -DHIPCC_BIN_DIR=\$HIPCC_DIR/bin -DHIP_COMMON_DIR=\$HIP_DIR -DCMAKE_INSTALL_PREFIX=\$PWD/install ..
                    fi
                    make -j\$(nproc)
                    make install  -j\$(nproc)
                    """
            }
        }
        
        stage("BUILD HIP TESTS - ${backendLabel}") {
            // Running the build on HIP TESTS workspace
            dir("${WORKSPACE}/hip-tests") {
                env.HIP_PATH = "${CLR_DIR}" + "/build/install"
                sh  """#!/usr/bin/env bash
                    set -x
                    rm -rf build
                    mkdir -p build
                    cd build
                    echo "testing $HIP_PATH"
                    # Check if backend label contains string "amd" or backend host is a server with amd gpu
                    if [[ $backendLabel =~ amd ]]; then
                        cmake -DHIP_PLATFORM=amd -DHIP_PATH=\$CLR_DIR/build/install ../catch 
                    else
                        export HIP_PLATFORM=nvidia
                        cmake -DHIP_PLATFORM=nvidia -DHIP_PATH=\$CLR_DIR/build/install ../catch
                    fi
                    make -j\$(nproc) build_tests
                    """
            }
        }

        timeout(time: 1, unit: 'HOURS') {
            stage("TEST - ${backendLabel}") {
                dir("${WORKSPACE}/hip-tests") {
                    sh  """#!/usr/bin/env bash
                        set -x
                        cd build
                    if [[ $backendLabel =~ amd ]]; then
                        ctest --overwrite BuildDirectory=. --output-junit hiptest_output_catch_amd.xml
                    else
                        ctest --overwrite BuildDirectory=. --output-junit hiptest_output_catch_nvidia.xml -E 'Unit_hipMemcpyHtoD_Positive_Synchronization_Behavior|Unit_hipMemcpy_Positive_Synchronization_Behavior|Unit_hipFreeNegativeHost'
                    fi
                    """
               }
            }
        }
    }
}

timestamps {
    node('external-bootstrap') {
        skipDefaultCheckout()

        // labels belonging to each backend - AMD, NVIDIA
        String[] labels = ['hip-amd-gfx908-ubu2004', 'hip-nvidia-rtx5000-ubu2004']
        buildMap = [:]

        labels.each { backendLabel ->
            echo "backendLabel: ${backendLabel}"
            buildMap[backendLabel] = { hipBuildTest(backendLabel) }
        }
        buildMap['failFast'] = false
        parallel  buildMap
    }
}
