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

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

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

            // Clone vdi and opencl for only amd backend server
            if (backendLabel =~ /.*amd.*/) {  
               dir("${WORKSPACE}/ROCm-OpenCL-Runtime") {
                   git branch:'develop',
                   url: 'https://github.com/RadeonOpenCompute/ROCm-OpenCL-Runtime'
                   env.OPENCL_DIR = "${WORKSPACE}" + "/ROCm-OpenCL-Runtime"
               }
               dir("${WORKSPACE}/ROCclr") {
                   git branch:'develop',
                   url: 'https://github.com/ROCm-Developer-Tools/ROCclr'
                   env.ROCclr_DIR = "${WORKSPACE}" + "/ROCclr"
               }
            }
        }

        stage("BUILD HIP - ${backendLabel}") {
            // Running the build on hipamd workspace
            dir("${WORKSPACE}/hipamd") {
                sh  """#!/usr/bin/env bash
                    set -x
                    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 -DHIP_PATH=\$PWD/install -DHIP_COMMON_DIR=\$HIP_DIR -DAMD_OPENCL_PATH=\$OPENCL_DIR -DROCCLR_PATH=\$ROCclr_DIR -DCMAKE_PREFIX_PATH="/opt/rocm/" -DCMAKE_INSTALL_PREFIX=\$PWD/install ..
                    else
                        cmake -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 = "${HIPAMD_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
                    """
            }
        }

        stage("TEST - ${backendLabel}") {
            dir("${WORKSPACE}/hip-tests") {
                sh  """#!/usr/bin/env bash
                    set -x
                    cd build
                    if [[ $backendLabel =~ amd ]]; then
                        ctest
                    else
                        ctest -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
    }
}
