kusano 2b45e8
#!/usr/bin/perl
kusano 2b45e8
kusano 2b45e8
# Checking cross compile
kusano 2b45e8
$hostos   = `uname -s | sed -e s/\-.*//`;    chop($hostos);
kusano 2b45e8
$hostarch = `uname -m | sed -e s/i.86/x86/`;chop($hostarch);
kusano 2b45e8
kusano 2b45e8
$binary = $ENV{"BINARY"};
kusano 2b45e8
kusano 2b45e8
$makefile = shift(@ARGV);
kusano 2b45e8
$config   = shift(@ARGV);
kusano 2b45e8
kusano 2b45e8
$compiler_name = join(" ", @ARGV);
kusano 2b45e8
kusano 2b45e8
# First, we need to know the target OS and compiler name
kusano 2b45e8
kusano 2b45e8
$data = `$compiler_name -E ctest.c`;
kusano 2b45e8
kusano 2b45e8
if ($?) {
kusano 2b45e8
    printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
kusano 2b45e8
    die 1;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
$cross_suffix = "";
kusano 2b45e8
kusano 2b45e8
if ($ARGV[0] =~ /(.*)(-[.\d]+)/) {
kusano 2b45e8
    if ($1 =~ /(.*-)(.*)/) {
kusano 2b45e8
	$cross_suffix = $1;
kusano 2b45e8
    }
kusano 2b45e8
} else {
kusano 2b45e8
    if ($ARGV[0] =~ /(.*-)(.*)/) {
kusano 2b45e8
	$cross_suffix = $1;
kusano 2b45e8
    }
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
$compiler = "";
kusano 2b45e8
$compiler = PGI       if ($data =~ /COMPILER_PGI/);
kusano 2b45e8
$compiler = PATHSCALE if ($data =~ /COMPILER_PATHSCALE/);
kusano 2b45e8
$compiler = INTEL     if ($data =~ /COMPILER_INTEL/);
kusano 2b45e8
$compiler = OPEN64    if ($data =~ /COMPILER_OPEN64/);
kusano 2b45e8
$compiler = SUN       if ($data =~ /COMPILER_SUN/);
kusano 2b45e8
$compiler = IBM       if ($data =~ /COMPILER_IBM/);
kusano 2b45e8
$compiler = DEC       if ($data =~ /COMPILER_DEC/);
kusano 2b45e8
$compiler = GCC       if ($compiler eq "");
kusano 2b45e8
kusano 2b45e8
$os = Linux           if ($data =~ /OS_LINUX/);
kusano 2b45e8
$os = FreeBSD         if ($data =~ /OS_FreeBSD/);
kusano 2b45e8
$os = NetBSD          if ($data =~ /OS_NetBSD/);
kusano 2b45e8
$os = Darwin          if ($data =~ /OS_Darwin/);
kusano 2b45e8
$os = SunOS           if ($data =~ /OS_SunOS/);
kusano 2b45e8
$os = AIX             if ($data =~ /OS_AIX/);
kusano 2b45e8
$os = osf             if ($data =~ /OS_OSF/);
kusano 2b45e8
$os = WINNT           if ($data =~ /OS_WINNT/);
kusano 2b45e8
$os = CYGWIN_NT       if ($data =~ /OS_CYGWIN/);
kusano 2b45e8
$os = Interix         if ($data =~ /OS_INTERIX/);
kusano 2b45e8
kusano 2b45e8
$architecture = x86    if ($data =~ /ARCH_X86/);
kusano 2b45e8
$architecture = x86_64 if ($data =~ /ARCH_X86_64/);
kusano 2b45e8
$architecture = power  if ($data =~ /ARCH_POWER/);
kusano 2b45e8
$architecture = mips32 if ($data =~ /ARCH_MIPS32/);
kusano 2b45e8
$architecture = mips64 if ($data =~ /ARCH_MIPS64/);
kusano 2b45e8
$architecture = alpha  if ($data =~ /ARCH_ALPHA/);
kusano 2b45e8
$architecture = sparc  if ($data =~ /ARCH_SPARC/);
kusano 2b45e8
$architecture = ia64   if ($data =~ /ARCH_IA64/);
kusano 2b45e8
kusano 2b45e8
$defined = 0;
kusano 2b45e8
kusano 2b45e8
if ($os eq "AIX") {
kusano 2b45e8
    $compiler_name .= " -maix32" if ($binary eq "32");
kusano 2b45e8
    $compiler_name .= " -maix64" if ($binary eq "64");
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if (($architecture eq "mips32") || ($architecture eq "mips64")) {
kusano 2b45e8
    $compiler_name .= " -mabi=n32" if ($binary eq "32");
kusano 2b45e8
    $compiler_name .= " -mabi=64" if ($binary eq "64");
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($architecture eq "alpha") {
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
    $binary = 64;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($architecture eq "ia64") {
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
    $binary = 64;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if (($architecture eq "x86") && ($os ne Darwin) && ($os ne SunOS)) {
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
    $binary =32;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "PGI") {
kusano 2b45e8
    $compiler_name .= " -tp p7"    if ($binary eq "32");
kusano 2b45e8
    $compiler_name .= " -tp p7-64" if ($binary eq "64");
kusano 2b45e8
    $openmp = "-mp";
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "IBM") {
kusano 2b45e8
    $compiler_name .= " -q32"  if ($binary eq "32");
kusano 2b45e8
    $compiler_name .= " -q64"  if ($binary eq "64");
kusano 2b45e8
    $openmp = "-qsmp=omp";
kusano 2b45e8
    $defined = 1;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "INTEL") {
kusano 2b45e8
    $openmp = "-openmp";
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "PATHSCALE") {
kusano 2b45e8
    $openmp = "-mp";
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "OPEN64") {
kusano 2b45e8
    $openmp = "-mp";
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "GCC") {
kusano 2b45e8
    $openmp = "-fopenmp";
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($defined == 0) {
kusano 2b45e8
    $compiler_name .= " -m32" if ($binary eq "32");
kusano 2b45e8
    $compiler_name .= " -m64" if ($binary eq "64");
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
# Do again
kusano 2b45e8
kusano 2b45e8
$data = `$compiler_name -E ctest.c`;
kusano 2b45e8
kusano 2b45e8
if ($?) {
kusano 2b45e8
    printf STDERR "C Compiler ($compiler_name) is something wrong.\n";
kusano 2b45e8
    die 1;
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
$architecture = x86    if ($data =~ /ARCH_X86/);
kusano 2b45e8
$architecture = x86_64 if ($data =~ /ARCH_X86_64/);
kusano 2b45e8
$architecture = power  if ($data =~ /ARCH_POWER/);
kusano 2b45e8
$architecture = mips32 if ($data =~ /ARCH_MIPS32/);
kusano 2b45e8
$architecture = mips64 if ($data =~ /ARCH_MIPS64/);
kusano 2b45e8
$architecture = alpha  if ($data =~ /ARCH_ALPHA/);
kusano 2b45e8
$architecture = sparc  if ($data =~ /ARCH_SPARC/);
kusano 2b45e8
$architecture = ia64   if ($data =~ /ARCH_IA64/);
kusano 2b45e8
kusano 2b45e8
$binformat    = bin32;
kusano 2b45e8
$binformat    = bin64  if ($data =~ /BINARY_64/);
kusano 2b45e8
kusano 2b45e8
$data = `$compiler_name -S ctest1.c && grep globl ctest1.s | head -n 1 && rm -f ctest1.s`;
kusano 2b45e8
kusano 2b45e8
$data =~ /globl\s([_\.]*)(.*)/;
kusano 2b45e8
kusano 2b45e8
$need_fu      = $1;
kusano 2b45e8
kusano 2b45e8
$cross = 0;
kusano 2b45e8
$cross = 1 if ($os ne $hostos);
kusano 2b45e8
kusano 2b45e8
if ($architecture ne $hostarch) {
kusano 2b45e8
    $cross = 1;
kusano 2b45e8
    $cross = 0 if (($hostarch eq "x86_64") && ($architecture eq "x86"));
kusano 2b45e8
    $cross = 0 if (($hostarch eq "mips64") && ($architecture eq "mips"));
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
$openmp = "" if $ENV{USE_OPENMP} != 1;
kusano 2b45e8
kusano 2b45e8
$linker_L = "";
kusano 2b45e8
$linker_l = "";
kusano 2b45e8
$linker_a = "";
kusano 2b45e8
kusano 2b45e8
{
kusano 2b45e8
    $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`;
kusano 2b45e8
    
kusano 2b45e8
    $link =~ s/\-Y\sP\,/\-Y/g;
kusano 2b45e8
    
kusano 2b45e8
    @flags = split(/[\s\,\n]/, $link);
kusano 2b45e8
    # remove leading and trailing quotes from each flag.
kusano 2b45e8
    @flags = map {s/^['"]|['"]$//g; $_} @flags;
kusano 2b45e8
kusano 2b45e8
    foreach $flags (@flags) {
kusano 2b45e8
	if (
kusano 2b45e8
	    ($flags =~ /^\-L/)
kusano 2b45e8
	    && ($flags !~ /^-LIST:/)
kusano 2b45e8
	    && ($flags !~ /^-LANG:/)
kusano 2b45e8
	    ) {
kusano 2b45e8
	    $linker_L .= $flags . " " 
kusano 2b45e8
	    }
kusano 2b45e8
kusano 2b45e8
	if ($flags =~ /^\-Y/) {
kusano 2b45e8
	    $linker_L .= "-Wl,". $flags . " " 
kusano 2b45e8
	    }
kusano 2b45e8
	
kusano 2b45e8
	if (
kusano 2b45e8
	    ($flags =~ /^\-l/) 
kusano 2b45e8
	    && ($flags !~ /gfortranbegin/)
kusano 2b45e8
	    && ($flags !~ /frtbegin/)
kusano 2b45e8
	    && ($flags !~ /pathfstart/)
kusano 2b45e8
	    && ($flags !~ /numa/)
kusano 2b45e8
	    && ($flags !~ /crt[0-9]/)
kusano 2b45e8
	    && ($flags !~ /gcc/)
kusano 2b45e8
	    && ($flags !~ /user32/)
kusano 2b45e8
	    && ($flags !~ /kernel32/)
kusano 2b45e8
	    && ($flags !~ /advapi32/)
kusano 2b45e8
	    && ($flags !~ /shell32/)
kusano 2b45e8
	    ) {
kusano 2b45e8
	    $linker_l .= $flags . " " 
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	$linker_a .= $flags . " " if $flags =~ /\.a$/;
kusano 2b45e8
    }
kusano 2b45e8
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
open(MAKEFILE, "> $makefile") || die "Can't create $makefile";
kusano 2b45e8
open(CONFFILE, "> $config"  ) || die "Can't create $config";
kusano 2b45e8
kusano 2b45e8
# print $data, "\n";
kusano 2b45e8
kusano 2b45e8
print MAKEFILE "OSNAME=$os\n";
kusano 2b45e8
print MAKEFILE "ARCH=$architecture\n";
kusano 2b45e8
print MAKEFILE "C_COMPILER=$compiler\n";
kusano 2b45e8
print MAKEFILE "BINARY32=\n" if $binformat ne bin32;
kusano 2b45e8
print MAKEFILE "BINARY64=\n" if $binformat ne bin64;
kusano 2b45e8
print MAKEFILE "BINARY32=1\n" if $binformat eq bin32;
kusano 2b45e8
print MAKEFILE "BINARY64=1\n" if $binformat eq bin64;
kusano 2b45e8
print MAKEFILE "FU=$need_fu\n" if $need_fu ne "";
kusano 2b45e8
print MAKEFILE "CROSS_SUFFIX=$cross_suffix\n" if $cross_suffix ne "";
kusano 2b45e8
print MAKEFILE "CROSS=1\n" if $cross != 0;
kusano 2b45e8
print MAKEFILE "CEXTRALIB=$linker_L $linker_l $linker_a\n";
kusano 2b45e8
kusano 2b45e8
$os           =~ tr/[a-z]/[A-Z]/;
kusano 2b45e8
$architecture =~ tr/[a-z]/[A-Z]/;
kusano 2b45e8
$compiler     =~ tr/[a-z]/[A-Z]/;
kusano 2b45e8
kusano 2b45e8
print CONFFILE "#define OS_$os\t1\n";
kusano 2b45e8
print CONFFILE "#define ARCH_$architecture\t1\n";
kusano 2b45e8
print CONFFILE "#define C_$compiler\t1\n";
kusano 2b45e8
print CONFFILE "#define __32BIT__\t1\n"  if $binformat eq bin32;
kusano 2b45e8
print CONFFILE "#define __64BIT__\t1\n"  if $binformat eq bin64;
kusano 2b45e8
print CONFFILE "#define FUNDERSCORE\t$need_fu\n" if $need_fu ne "";
kusano 2b45e8
kusano 2b45e8
if ($os eq "LINUX") {
kusano 2b45e8
    
kusano 2b45e8
    @pthread = split(/\s+/, `nm /lib/libpthread.so* | grep _pthread_create`);
kusano 2b45e8
    
kusano 2b45e8
    if ($pthread[2] ne "") {
kusano 2b45e8
	print CONFFILE "#define PTHREAD_CREATE_FUNC	$pthread[2]\n";
kusano 2b45e8
    } else {
kusano 2b45e8
	print CONFFILE "#define PTHREAD_CREATE_FUNC	pthread_create\n";
kusano 2b45e8
    }
kusano 2b45e8
} else {
kusano 2b45e8
    print CONFFILE "#define PTHREAD_CREATE_FUNC	pthread_create\n";
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
close(MAKEFILE);
kusano 2b45e8
close(CONFFILE);