Blob Blame Raw
#!/usr/bin/perl

#
# 1. Not specified
#   1.1 Automatically detect, then check compiler
#   1.2 If no fortran compiler is detected, g77 is default with NOFORTRAN definition
# 2. Specified
#   2.1 If path is correct, check compiler
#   2.2 If path is not correct, but still valid compiler name, force setting
#     2.2.2 Path is not correct, invalid compiler name, then g77 is default with NOFORTRAN definition
#

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

$nofortran = 0;

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

# f77 is too ambiguous
$compiler = "" if $compiler eq "f77";

@path = split(/:/, $ENV{"PATH"});

if ($compiler eq "") {

    @lists = ("f77", "g77", "g95", "gfortran", "frt", "fort", "openf90", "openf95",
	      "sunf77", "sunf90", "sunf95",
              "xlf95", "xlf90", "xlf", 
              "ppuf77", "ppuf95", "ppuf90", "ppuxlf", 
	      "pathf90", "pathf95", 
	      "pgf95", "pgf90", "pgf77",
              "ifort");
    
    foreach $lists (@lists) {
        foreach $path (@path) {
            if (-f $path . "/" . $lists) {
                $compiler = $lists;
                break;
            }
        }
    }

}

if ($compiler eq "") {

    $nofortran = 1;
    $compiler = "g77";
    $vendor = G77;
    $bu       = "_";

} else {

    $data = `which $compiler > /dev/null 2> /dev/null`;
    $vendor = "";

    if (!$?) {

	$data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.s && rm -f ftest.s`;

	if ($data =~ /zhoge_/) {
	    $bu       = "_";
	}

	if ($data =~ /GNU/) {

	    $data =~ /(\d)\.(\d).(\d)/;
	    $major = $1;
	    $minor = $2;

	    if ($major >= 4) {
		$vendor = GFORTRAN;
		$openmp = "-fopenmp";
	    } else {
		$vendor = G77;
		$openmp = "";
	    }

	}

	if ($data =~ /g95/) {
	    $vendor = G95;
	    $openmp = "";
	}

	if ($data =~ /Intel/) {
	    $vendor = INTEL;
	    $openmp = "-openmp";
	}

        if ($data =~ /Sun Fortran/) {
            $vendor = SUN;
	    $openmp = "-xopenmp=parallel";
        }

	if ($data =~ /PathScale/) {
	    $vendor = PATHSCALE;
	    $openmp = "-openmp";
	}

	if ($data =~ /Open64/) {
	    $vendor = OPEN64;
	    $openmp = "-mp";
	}

	if ($data =~ /PGF/) {
	    $vendor = PGI;
	    $openmp = "-mp";
	}

	if ($data =~ /IBM/) {
	    $vendor = IBM;
	    $openmp = "-openmp";
	}
    }

    if ($vendor eq "") {

	if ($compiler =~ /g77/) {
	    $vendor = G77;
	    $bu       = "_";
	    $openmp = "";
	}
	
	if ($compiler =~ /g95/) {
	    $vendor = G95;
	    $bu       = "_";
	    $openmp = "";
	}
	
	if ($compiler =~ /gfortran/) {
	    $vendor = GFORTRAN;
	    $bu       = "_";
	    $openmp = "-fopenmp";
	}
	
	if ($compiler =~ /ifort/) {
	    $vendor = INTEL;
	    $bu       = "_";
	    $openmp = "-openmp";
	}
	
	if ($compiler =~ /pathf/) {
	    $vendor = PATHSCALE;
	    $bu       = "_";
	    $openmp = "-mp";
	}
	
	if ($compiler =~ /pgf/) {
	    $vendor = PGI;
	    $bu       = "_";
	    $openmp = "-mp";
	}
	
	if ($compiler =~ /ftn/) {
	    $vendor = PGI;
	    $bu       = "_";
	    $openmp = "-openmp";
	}
	
	if ($compiler =~ /frt/) {
	    $vendor = FUJITSU;
	    $bu       = "_";
	    $openmp = "-openmp";
	}

	if ($compiler =~ /sunf77|sunf90|sunf95/) {
	    $vendor = SUN;
	    $bu       = "_";
	    $openmp = "-xopenmp=parallel";
	}
	
	if ($compiler =~ /ppuf/) {
	    $vendor = IBM;
	    $openmp = "-openmp";
	}
	
	if ($compiler =~ /xlf/) {
	    $vendor = IBM;
	    $openmp = "-openmp";
	}

	if ($compiler =~ /open64/) {
	    $vendor = OPEN64;
	    $openmp = "-mp";
	}

	if ($vendor eq "") {
	    $nofortran = 1;
	    $compiler = "g77";
	    $vendor = G77;
	    $bu       = "_";
	    $openmp = "";
	}

    }
}

$data = `which $compiler > /dev/null 2> /dev/null`;

if (!$?) {

    $binary = $ENV{"BINARY"};
    
    $openmp = "" if $ENV{USE_OPENMP} != 1;
    
    if ($binary == 32) {
	$link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
	if ($?) {
	    $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
	}
	$binary = "" if ($?);
    }
    
    if ($binary == 64) {
	$link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
	if ($?) {
	    $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
	}
	$binary = "" if ($?);
    }
    
    if ($binary eq "") {
	$link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
    }
}
    
$linker_L = "";
$linker_l = "";
$linker_a = "";

if ($link ne "") {

    $link =~ s/\-Y\sP\,/\-Y/g;

    $link =~ s/\-rpath\s+/\-rpath\@/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:/)
	    ) {
	    if ($vendor eq "PGI") {
		$flags =~ s/lib$/libso/;
	    }
	    $linker_L .= $flags . " ";
	}
    
	if ($flags =~ /^\-Y/) {
	    $linker_L .= "-Wl,". $flags . " ";
	    }
	
	if ($flags =~ /^\-rpath/) {
	    $flags =~ s/\@/\,/g;
	    if ($vendor eq "PGI") {
		$flags =~ s/lib$/libso/;
	    }
	    $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/)
		&& ($flags !~ /^\-l$/)
	    ) {
	    $linker_l .= $flags . " ";
	}

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

}

if ($vendor eq "INTEL"){
    $linker_a .= "-lgfortran"
}

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

print MAKEFILE "F_COMPILER=$vendor\n";
print MAKEFILE "FC=$compiler\n";
print MAKEFILE "BU=$bu\n" if $bu ne "";
print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;

print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";

if (($linker_l ne "") || ($linker_a ne "")) {
    print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
}

close(MAKEFILE);
close(CONFFILE);