#!/usr/bin/perl -w
use Getopt::Long;
use Cwd;

# Return name of HIP compiler - either 'nvcc' or 'hcc'
#
use Getopt::Long;
use File::Basename;

Getopt::Long::Configure ( qw{bundling no_ignore_case});
GetOptions(
     "help|h" => \$p_help
    ,"path|p" => \$p_path
    ,"compiler|c" => \$p_compiler
    ,"platform|P" => \$p_platform
    ,"cpp_config|cxx_config|C" => \$p_cpp_config
    ,"full|f|info" => \$p_full,
    ,"check" => \$p_check,
    ,"newline|n" => \$p_newline
);

if ($p_help) {
    print "usage: hipconfig [OPTIONS]\n";
    print "  --path,  -p        : print HIP_PATH (use env var if set, else determine from hipconfig path)\n";
    print "  --cpp_config, -C   : print C++ compiler options\n";
    print "  --compiler, -c     : print compiler (hcc or nvcc)\n";
    print "  --platform, -P     : print platform (hcc or nvcc)\n";
    print "  --full, -f         : print full config\n";
    print "  --check            : check configuration\n";
    print "  --newline, -n      : print newline\n";
    print "  --help, -h         : print help message\n";
    exit();
}


$CUDA_PATH=$ENV{'CUDA_PATH'};
$CUDA_PATH='/usr/local/cuda' unless defined $CUDA_PATH;

$HCC_HOME=$ENV{'HCC_HOME'};
$HCC_HOME='/opt/rocm/hcc' unless defined $HCC_HOME;

$HSA_PATH=$ENV{'HSA_PATH'};
$HSA_PATH='/opt/rocm/hsa' unless defined $HSA_PATH;

#---
#HIP_PLATFORM controls whether to use NVCC or HCC for compilation:
$HIP_PLATFORM=$ENV{'HIP_PLATFORM'};
if (not defined $HIP_PLATFORM) {
    $NAMDGPUNODES=`cat /sys/class/kfd/kfd/topology/nodes/*/properties 2>/dev/null | grep -c 'simd_count [1-9]'`;
    
    if ($NAMDGPUNODES > 0) {
        $HIP_PLATFORM = "hcc"
    } else {
        $HIP_PLATFORM = "nvcc";
    }
}

$HIP_PATH=$ENV{'HIP_PATH'};
$HIP_PATH=Cwd::realpath (dirname (dirname $0)) unless defined $HIP_PATH;        # use parent directory of this tool


if ($HIP_PLATFORM eq "hcc") {
    $CPP_CONFIG= " -D__HIP_PLATFORM_HCC__=   -I$HIP_PATH/include -I$HCC_HOME/include";
}
if ($HIP_PLATFORM eq "nvcc") {
    $CPP_CONFIG = " -D__HIP_PLATFORM_NVCC__=  -I$HIP_PATH/include -I$CUDA_PATH/include";
};

if ($p_path) {
    print "$HIP_PATH";
    $printed = 1;
}
    

if ($p_cpp_config) {
    print $CPP_CONFIG;
    $printed = 1;
}


if ($p_compiler or $p_platform) {
    print $HIP_PLATFORM;
    $printed = 1;
}



if (!$printed or $p_full) {
    print "== hipconfig\n";
    print "HIP_PATH     : ", $HIP_PATH, "\n";
    print "HIP_PLATFORM : ", $HIP_PLATFORM, "\n";
    print "CPP_CONFIG   : ", $CPP_CONFIG, "\n";
    if ($HIP_PLATFORM eq "hcc") 
    {
        print "\n" ;
        print "== hcc\n";
        print ("HSA_PATH     : $HSA_PATH\n");
        print ("HCC_HOME     : $HCC_HOME\n");
        system("$HCC_HOME/bin/hcc --version");
        print ("HCC-cxxflags: "); 
        system("$HCC_HOME/bin/hcc-config --cxxflags");
        print ("HCC-ldflags : "); 
        system("$HCC_HOME/bin/hcc-config --ldflags");
        printf("\n");
    }
    if ($HIP_PLATFORM eq "nvcc")  {
        print "\n" ;
        print "== nvcc\n";
        #print "CUDA_PATH   :", $CUDA_PATH";
        system("nvcc --version");

    }
    print "\n" ;

    print "=== Environment Variables\n";
    system("echo PATH=\$PATH");
    system("env | egrep '^HIP|^HSA|^HCC|^CUDA|^LD_LIBRARY_PATH'");


    print "\n" ;
    print "== Linux Kernel\n";
    system ("uname -a");

    if (-e "/usr/bin/lsb_release") {
        system ("/usr/bin/lsb_release -a");
    }

    print "\n" ;
    $printed = 1;
}


if ($p_check) {
    print "\nCheck system installation:\n";

    printf ("%-70s", "check hipconfig in PATH...");
    if (system ("hipconfig > /dev/null 2>&1") != 0)  {
        print "FAIL\n";
    } else {
        printf "good\n";
    }

    if ($HIP_PLATFORM eq "hcc")  {
        $LD_LIBRARY_PATH=$ENV{'LD_LIBRARY_PATH'};
        printf("%-70s", "check LD_LIBRARY_PATH ($LD_LIBRARY_PATH) contains HSA_PATH ($HSA_PATH)...");
        if (index($LD_LIBRARY_PATH, $HSA_PATH) == -1) {
            print "FAIL\n";
        } else {
            printf "good\n";
        }

        # TODO - check hipcc / nvcc found and executable.
    }
}


if ($p_newline) {
    print "\n";
}
