def hipBuildTest(String backendLabel) {
        node(backendLabel) {
          stage("Source sync ${backendLabel}") {

            // Checkout hip repository with the PR patch
            dir("${WORKSPACE}/HIPCC") {
                   checkout scm
                   env.HIPCC_DIR = "${WORKSPACE}" + "/HIPCC"
            }
            
            // Clone HIP repository
            dir("${WORKSPACE}/hip") {
               git branch: 'develop',
               url: 'https://github.com/ROCm-Developer-Tools/HIP'
               env.HIP_DIR = "${WORKSPACE}" + "/hip"
            }

            // 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 hip-tests repository
            dir("${WORKSPACE}/hip-tests") {
              git branch: 'develop',
              url: 'https://github.com/ROCm-Developer-Tools/hip-tests'
            }
        }

        stage("Build - HIPCC") {
            // Running the build on HIPCC workspace
            dir("${WORKSPACE}/HIPCC") {
                sh  """#!/usr/bin/env bash
                    set -x
                    rm -rf build
                    mkdir build
                    cd build/
                    cmake ..
                    make
                    """
            }
        }

        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 -DHIPCC_BIN_DIR=\$HIPCC_DIR/bin -DCLR_BUILD_HIP=ON -DHIP_PATH=\$PWD/install -DHIP_COMMON_DIR=\$HIP_DIR -DCMAKE_PREFIX_PATH="/opt/rocm/" -DCMAKE_INSTALL_PREFIX=\$PWD/install ..
                    else
                        cmake -DHIPCC_BIN_DIR=\$HIPCC_DIR/bin -DCLR_BUILD_HIP=ON -DHIP_PLATFORM=nvidia -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 ../catch 
                    else
                        export HIP_PLATFORM=nvidia
                        cmake -DHIP_PLATFORM=nvidia ../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-vg20-ubu1804', 'hip-nvidia-rtx5000-ubu1804']
        buildMap = [:]

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