Blob Blame Raw
#!/usr/bin/perl

# Checking cross compile
$hostos   = `uname -s | sed -e s/\-.*//`;    chop($hostos);
$hostarch = `uname -m | sed -e s/i.86/x86/`;chop($hostarch);

$binary = $ENV{"BINARY"};

$makefile = shift(@ARGV);
$config   = shift(@ARGV);

$compiler_name = join(" ", @ARGV);

# First, we need to know the target OS and compiler name

$data = `$compiler_name -E ctest.c`;

if ($?) {
    printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
    die 1;
}

$cross_suffix = "";

if ($ARGV[0] =~ /(.*)(-[.\d]+)/) {
    if ($1 =~ /(.*-)(.*)/) {
	$cross_suffix = $1;
    }
} else {
    if ($ARGV[0] =~ /(.*-)(.*)/) {
	$cross_suffix = $1;
    }
}

$compiler = "";
$compiler = PGI       if ($data =~ /COMPILER_PGI/);
$compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
$compiler = INTEL     if ($data =~ /COMPILER_INTEL/);
$compiler = OPEN64    if ($data =~ /COMPILER_OPEN64/);
$compiler = SUN       if ($data =~ /COMPILER_SUN/);
$compiler = IBM       if ($data =~ /COMPILER_IBM/);
$compiler = DEC       if ($data =~ /COMPILER_DEC/);
$compiler = GCC       if ($compiler eq "");

$os = Linux           if ($data =~ /OS_LINUX/);
$os = FreeBSD         if ($data =~ /OS_FreeBSD/);
$os = NetBSD          if ($data =~ /OS_NetBSD/);
$os = Darwin          if ($data =~ /OS_Darwin/);
$os = SunOS           if ($data =~ /OS_SunOS/);
$os = AIX             if ($data =~ /OS_AIX/);
$os = osf             if ($data =~ /OS_OSF/);
$os = WINNT           if ($data =~ /OS_WINNT/);
$os = CYGWIN_NT       if ($data =~ /OS_CYGWIN/);
$os = Interix         if ($data =~ /OS_INTERIX/);

$architecture = x86    if ($data =~ /ARCH_X86/);
$architecture = x86_64 if ($data =~ /ARCH_X86_64/);
$architecture = power  if ($data =~ /ARCH_POWER/);
$architecture = mips32 if ($data =~ /ARCH_MIPS32/);
$architecture = mips64 if ($data =~ /ARCH_MIPS64/);
$architecture = alpha  if ($data =~ /ARCH_ALPHA/);
$architecture = sparc  if ($data =~ /ARCH_SPARC/);
$architecture = ia64   if ($data =~ /ARCH_IA64/);

$defined = 0;

if ($os eq "AIX") {
    $compiler_name .= " -maix32" if ($binary eq "32");
    $compiler_name .= " -maix64" if ($binary eq "64");
    $defined = 1;
}

if (($architecture eq "mips32") || ($architecture eq "mips64")) {
    $compiler_name .= " -mabi=n32" if ($binary eq "32");
    $compiler_name .= " -mabi=64" if ($binary eq "64");
    $defined = 1;
}

if ($architecture eq "alpha") {
    $defined = 1;
    $binary = 64;
}

if ($architecture eq "ia64") {
    $defined = 1;
    $binary = 64;
}

if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
    $defined = 1;
    $binary =32;
}

if ($compiler eq "PGI") {
    $compiler_name .= " -tp p7"    if ($binary eq "32");
    $compiler_name .= " -tp p7-64" if ($binary eq "64");
    $openmp = "-mp";
    $defined = 1;
}

if ($compiler eq "IBM") {
    $compiler_name .= " -q32"  if ($binary eq "32");
    $compiler_name .= " -q64"  if ($binary eq "64");
    $openmp = "-qsmp=omp";
    $defined = 1;
}

if ($compiler eq "INTEL") {
    $openmp = "-openmp";
}

if ($compiler eq "PATHSCALE") {
    $openmp = "-mp";
}

if ($compiler eq "OPEN64") {
    $openmp = "-mp";
}

if ($compiler eq "GCC") {
    $openmp = "-fopenmp";
}

if ($defined == 0) {
    $compiler_name .= " -m32" if ($binary eq "32");
    $compiler_name .= " -m64" if ($binary eq "64");
}

# Do again

$data = `$compiler_name -E ctest.c`;

if ($?) {
    printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
    die 1;
}

$architecture = x86    if ($data =~ /ARCH_X86/);
$architecture = x86_64 if ($data =~ /ARCH_X86_64/);
$architecture = power  if ($data =~ /ARCH_POWER/);
$architecture = mips32 if ($data =~ /ARCH_MIPS32/);
$architecture = mips64 if ($data =~ /ARCH_MIPS64/);
$architecture = alpha  if ($data =~ /ARCH_ALPHA/);
$architecture = sparc  if ($data =~ /ARCH_SPARC/);
$architecture = ia64   if ($data =~ /ARCH_IA64/);

$binformat    = bin32;
$binformat    = bin64  if ($data =~ /BINARY_64/);

$data = `$compiler_name -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;

$data =~ /globl\s([_\.]*)(.*)/;

$need_fu      = $1;

$cross = 0;
$cross = 1 if ($os ne $hostos);

if ($architecture ne $hostarch) {
    $cross = 1;
    $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
    $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
}

$openmp = "" if $ENV{USE_OPENMP} != 1;

$linker_L = "";
$linker_l = "";
$linker_a = "";

{
    $link = `$compiler_name -c ctest2.c -o ctest2.o 2>&1 && $compiler_name $openmp -v ctest2.o -o ctest2 2>&1 && rm -f ctest2.o ctest2 ctest2.exe`;
    
    $link =~ s/\-Y\sP\,/\-Y/g;
    
    @flags = split(/[\s\,\n]/, $link);
    # remove leading and trailing quotes from each flag.
    @flags = map {s/^['"]|['"]$//g; $_} @flags;

    foreach $flags (@flags) {
	if (
	    ($flags =~ /^\-L/)
	    && ($flags !~ /^-LIST:/)
	    && ($flags !~ /^-LANG:/)
	    ) {
	    $linker_L .= $flags . " " 
	    }

	if ($flags =~ /^\-Y/) {
	    $linker_L .= "-Wl,". $flags . " " 
	    }
	
	if (
	    ($flags =~ /^\-l/) 
	    && ($flags !~ /gfortranbegin/)
	    && ($flags !~ /frtbegin/)
	    && ($flags !~ /pathfstart/)
	    && ($flags !~ /numa/)
	    && ($flags !~ /crt[0-9]/)
	    && ($flags !~ /gcc/)
	    && ($flags !~ /user32/)
	    && ($flags !~ /kernel32/)
	    && ($flags !~ /advapi32/)
	    && ($flags !~ /shell32/)
	    ) {
	    $linker_l .= $flags . " " 
	}

	$linker_a .= $flags . " " if $flags =~ /\.a$/;
    }

}

open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
open(CONFFILE, "> $config"  ) || die "Can't create $config";

# print $data, "\n";

print MAKEFILE "OSNAME=$os\n";
print MAKEFILE "ARCH=$architecture\n";
print MAKEFILE "C_COMPILER=$compiler\n";
print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross_suffix ne "";
print MAKEFILE "CROSS=1\n" if $cross != 0;
print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";

$os           =~ tr/[a-z]/[A-Z]/;
$architecture =~ tr/[a-z]/[A-Z]/;
$compiler     =~ tr/[a-z]/[A-Z]/;

print CONFFILE "#define OS_$os\t1\n";
print CONFFILE "#define ARCH_$architecture\t1\n";
print CONFFILE "#define C_$compiler\t1\n";
print CONFFILE "#define __32BIT__\t1\n"  if $binformat eq bin32;
print CONFFILE "#define __64BIT__\t1\n"  if $binformat eq bin64;
print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";

if ($os eq "LINUX") {
    
    @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
    
    if ($pthread[2] ne "") {
	print CONFFILE "#define PTHREAD_CREATE_FUNC	$pthread[2]\n";
    } else {
	print CONFFILE "#define PTHREAD_CREATE_FUNC	pthread_create\n";
    }
} else {
    print CONFFILE "#define PTHREAD_CREATE_FUNC	pthread_create\n";
}

close(MAKEFILE);
close(CONFFILE);