kusano 2b45e8
#!/usr/bin/perl
kusano 2b45e8
kusano 2b45e8
#
kusano 2b45e8
# 1. Not specified
kusano 2b45e8
#   1.1 Automatically detect, then check compiler
kusano 2b45e8
#   1.2 If no fortran compiler is detected, g77 is default with NOFORTRAN definition
kusano 2b45e8
# 2. Specified
kusano 2b45e8
#   2.1 If path is correct, check compiler
kusano 2b45e8
#   2.2 If path is not correct, but still valid compiler name, force setting
kusano 2b45e8
#     2.2.2 Path is not correct, invalid compiler name, then g77 is default with NOFORTRAN definition
kusano 2b45e8
#
kusano 2b45e8
kusano 2b45e8
$makefile = shift(@ARGV);
kusano 2b45e8
$config   = shift(@ARGV);
kusano 2b45e8
kusano 2b45e8
$nofortran = 0;
kusano 2b45e8
kusano 2b45e8
$compiler = join(" ", @ARGV);
kusano 2b45e8
kusano 2b45e8
# f77 is too ambiguous
kusano 2b45e8
$compiler = "" if $compiler eq "f77";
kusano 2b45e8
kusano 2b45e8
@path = split(/:/, $ENV{"PATH"});
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "") {
kusano 2b45e8
kusano 2b45e8
    @lists = ("f77", "g77", "g95", "gfortran", "frt", "fort", "openf90", "openf95",
kusano 2b45e8
	      "sunf77", "sunf90", "sunf95",
kusano 2b45e8
              "xlf95", "xlf90", "xlf", 
kusano 2b45e8
              "ppuf77", "ppuf95", "ppuf90", "ppuxlf", 
kusano 2b45e8
	      "pathf90", "pathf95", 
kusano 2b45e8
	      "pgf95", "pgf90", "pgf77",
kusano 2b45e8
              "ifort");
kusano 2b45e8
    
kusano 2b45e8
    foreach $lists (@lists) {
kusano 2b45e8
        foreach $path (@path) {
kusano 2b45e8
            if (-f $path . "/" . $lists) {
kusano 2b45e8
                $compiler = $lists;
kusano 2b45e8
                break;
kusano 2b45e8
            }
kusano 2b45e8
        }
kusano 2b45e8
    }
kusano 2b45e8
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
if ($compiler eq "") {
kusano 2b45e8
kusano 2b45e8
    $nofortran = 1;
kusano 2b45e8
    $compiler = "g77";
kusano 2b45e8
    $vendor = G77;
kusano 2b45e8
    $bu       = "_";
kusano 2b45e8
kusano 2b45e8
} else {
kusano 2b45e8
kusano 2b45e8
    $data = `which $compiler > /dev/null 2> /dev/null`;
kusano 2b45e8
    $vendor = "";
kusano 2b45e8
kusano 2b45e8
    if (!$?) {
kusano 2b45e8
kusano 2b45e8
	$data = `$compiler -O2 -S ftest.f > /dev/null 2>&1 && cat ftest.s && rm -f ftest.s`;
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /zhoge_/) {
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /GNU/) {
kusano 2b45e8
kusano 2b45e8
	    $data =~ /(\d)\.(\d).(\d)/;
kusano 2b45e8
	    $major = $1;
kusano 2b45e8
	    $minor = $2;
kusano 2b45e8
kusano 2b45e8
	    if ($major >= 4) {
kusano 2b45e8
		$vendor = GFORTRAN;
kusano 2b45e8
		$openmp = "-fopenmp";
kusano 2b45e8
	    } else {
kusano 2b45e8
		$vendor = G77;
kusano 2b45e8
		$openmp = "";
kusano 2b45e8
	    }
kusano 2b45e8
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /g95/) {
kusano 2b45e8
	    $vendor = G95;
kusano 2b45e8
	    $openmp = "";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /Intel/) {
kusano 2b45e8
	    $vendor = INTEL;
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
        if ($data =~ /Sun Fortran/) {
kusano 2b45e8
            $vendor = SUN;
kusano 2b45e8
	    $openmp = "-xopenmp=parallel";
kusano 2b45e8
        }
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /PathScale/) {
kusano 2b45e8
	    $vendor = PATHSCALE;
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /Open64/) {
kusano 2b45e8
	    $vendor = OPEN64;
kusano 2b45e8
	    $openmp = "-mp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /PGF/) {
kusano 2b45e8
	    $vendor = PGI;
kusano 2b45e8
	    $openmp = "-mp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($data =~ /IBM/) {
kusano 2b45e8
	    $vendor = IBM;
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
    }
kusano 2b45e8
kusano 2b45e8
    if ($vendor eq "") {
kusano 2b45e8
kusano 2b45e8
	if ($compiler =~ /g77/) {
kusano 2b45e8
	    $vendor = G77;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /g95/) {
kusano 2b45e8
	    $vendor = G95;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /gfortran/) {
kusano 2b45e8
	    $vendor = GFORTRAN;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-fopenmp";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /ifort/) {
kusano 2b45e8
	    $vendor = INTEL;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /pathf/) {
kusano 2b45e8
	    $vendor = PATHSCALE;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-mp";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /pgf/) {
kusano 2b45e8
	    $vendor = PGI;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-mp";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /ftn/) {
kusano 2b45e8
	    $vendor = PGI;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /frt/) {
kusano 2b45e8
	    $vendor = FUJITSU;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($compiler =~ /sunf77|sunf90|sunf95/) {
kusano 2b45e8
	    $vendor = SUN;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "-xopenmp=parallel";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /ppuf/) {
kusano 2b45e8
	    $vendor = IBM;
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
	
kusano 2b45e8
	if ($compiler =~ /xlf/) {
kusano 2b45e8
	    $vendor = IBM;
kusano 2b45e8
	    $openmp = "-openmp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($compiler =~ /open64/) {
kusano 2b45e8
	    $vendor = OPEN64;
kusano 2b45e8
	    $openmp = "-mp";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
	if ($vendor eq "") {
kusano 2b45e8
	    $nofortran = 1;
kusano 2b45e8
	    $compiler = "g77";
kusano 2b45e8
	    $vendor = G77;
kusano 2b45e8
	    $bu       = "_";
kusano 2b45e8
	    $openmp = "";
kusano 2b45e8
	}
kusano 2b45e8
kusano 2b45e8
    }
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
$data = `which $compiler > /dev/null 2> /dev/null`;
kusano 2b45e8
kusano 2b45e8
if (!$?) {
kusano 2b45e8
kusano 2b45e8
    $binary = $ENV{"BINARY"};
kusano 2b45e8
    
kusano 2b45e8
    $openmp = "" if $ENV{USE_OPENMP} != 1;
kusano 2b45e8
    
kusano 2b45e8
    if ($binary == 32) {
kusano 2b45e8
	$link = `$compiler $openmp -m32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
kusano 2b45e8
	if ($?) {
kusano 2b45e8
	    $link = `$compiler $openmp -q32 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
kusano 2b45e8
	}
kusano 2b45e8
	$binary = "" if ($?);
kusano 2b45e8
    }
kusano 2b45e8
    
kusano 2b45e8
    if ($binary == 64) {
kusano 2b45e8
	$link = `$compiler $openmp -m64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
kusano 2b45e8
	if ($?) {
kusano 2b45e8
	    $link = `$compiler $openmp -q64 -v ftest2.f 2>&1 && rm -f a.out a.exe`;
kusano 2b45e8
	}
kusano 2b45e8
	$binary = "" if ($?);
kusano 2b45e8
    }
kusano 2b45e8
    
kusano 2b45e8
    if ($binary eq "") {
kusano 2b45e8
	$link = `$compiler $openmp -v ftest2.f 2>&1 && rm -f a.out a.exe`;
kusano 2b45e8
    }
kusano 2b45e8
}
kusano 2b45e8
    
kusano 2b45e8
$linker_L = "";
kusano 2b45e8
$linker_l = "";
kusano 2b45e8
$linker_a = "";
kusano 2b45e8
kusano 2b45e8
if ($link ne "") {
kusano 2b45e8
kusano 2b45e8
    $link =~ s/\-Y\sP\,/\-Y/g;
kusano 2b45e8
kusano 2b45e8
    $link =~ s/\-rpath\s+/\-rpath\@/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
	    if ($vendor eq "PGI") {
kusano 2b45e8
		$flags =~ s/lib$/libso/;
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 ($flags =~ /^\-rpath/) {
kusano 2b45e8
	    $flags =~ s/\@/\,/g;
kusano 2b45e8
	    if ($vendor eq "PGI") {
kusano 2b45e8
		$flags =~ s/lib$/libso/;
kusano 2b45e8
	    }
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
		&& ($flags !~ /^\-l$/)
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
if ($vendor eq "INTEL"){
kusano 2b45e8
    $linker_a .= "-lgfortran"
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
open(MAKEFILE, ">> $makefile") || die "Can't append $makefile";
kusano 2b45e8
open(CONFFILE, ">> $config"  ) || die "Can't append $config";
kusano 2b45e8
kusano 2b45e8
print MAKEFILE "F_COMPILER=$vendor\n";
kusano 2b45e8
print MAKEFILE "FC=$compiler\n";
kusano 2b45e8
print MAKEFILE "BU=$bu\n" if $bu ne "";
kusano 2b45e8
print MAKEFILE "NOFORTRAN=1\n" if $nofortran == 1;
kusano 2b45e8
kusano 2b45e8
print CONFFILE "#define BUNDERSCORE\t$bu\n" if $bu ne "";
kusano 2b45e8
print CONFFILE "#define NEEDBUNDERSCORE\t1\n" if $bu ne "";
kusano 2b45e8
kusano 2b45e8
if (($linker_l ne "") || ($linker_a ne "")) {
kusano 2b45e8
    print MAKEFILE "FEXTRALIB=$linker_L $linker_l $linker_a\n";
kusano 2b45e8
}
kusano 2b45e8
kusano 2b45e8
close(MAKEFILE);
kusano 2b45e8
close(CONFFILE);