#!/usr/bin/env python
#
# Copyright (c) 2006-2007 The ABINIT Group (Yann Pouillon)
# All rights reserved.
#
# This file is part of the ABINIT software package. For license information,
# please see the COPYING file in the top-level directory of the ABINIT source
# distribution.
#

from time import gmtime,strftime

import commands
import os
import re
import sys

# ---------------------------------------------------------------------------- #

#
# Subroutines
#

# Macro header
def macro_header(name,stamp):

 return """# Generated by %s on %s

#
# Auto-detection of optimized linear algebra libraries
#

#
# IMPORTANT NOTE
#
# This file has been automatically generated by the %s
# script. Any change will systematically be overwritten.
#
""" % (name,stamp,name)



# Library macro header
def macro_lib(name,name32,name64,dirs32,dirs64):

 ret = """


# _ABI_CHECK_LIB_%s()
# -----------------%s
#
# Looks for the %s library.
#
AC_DEFUN([_ABI_CHECK_LIB_%s],
[dnl Set search paths
 abi_check_dirs="/usr/local/lib"
 case "${target_os}" in
  irix*|macosx)
   abi_check_dirs="/sw/lib ${abi_check_dirs}"
   ;;
 esac
""" % (name.upper(),re.sub(".","-",name),name.upper(),name.upper())

 ret += "\n dnl Set library names\n"

 if (name32 == "" ):
  ret += " lib_blas=\"blas\"\n lib_lapack=\"lapack\"\n" + \
   " lib_dirs=\"%s ${abi_check_libs}\"\n" % (dirs32)
 elif ( name32 == name64 ):
  ret += " lib_blas=\"%s\"\n lib_lapack=\"%s\"\n" % (name32,name32) + \
   " lib_dirs=\"%s ${abi_check_libs}\"\n" % (dirs32)
 else:
  ret += """ case "${abi_cpu_bits}" in
  32)
   lib_blas="%s"
   lib_lapack="%s"
   lib_dirs="%s ${abi_check_dirs}"
   ;;
  64)
   lib_blas="%s"
   lib_lapack="%s"
   lib_dirs="%s ${abi_check_dirs}"
   ;;
  *)
   lib_blas=""
   lib_lapack=""
   ;;
 esac
""" % (name32,name32,dirs32,name64,name64,dirs64)

 ret += """
 if test "${lib_blas}" != "" -a "${lib_lapack}" != ""; then
  for dir in ${lib_dirs} none; do
   dnl Clear cache values
   unset ac_cv_search_dgemm
   unset ac_cv_search_zggev

   dnl Look for the library routines
   if test "${dir}" = "none"; then
    AC_MSG_NOTICE([looking inside system paths])
    LDFLAGS=""
   else
    AC_MSG_NOTICE([looking inside ${dir}])
    LDFLAGS="-L${dir}"
   fi
   AC_CHECK_LIB([${lib_blas}],[dgemm],[abi_la_blas="%s"])
   AC_CHECK_LIB([${lib_lapack}],[zggev],[abi_la_lapack="%s"])

   if test "${abi_la_blas}" != "" -a "${abi_la_lapack}" != ""; then
    break
   fi
  done
 fi
]) # _ABI_CHECK_LIB_%s
""" % (name,name,name.upper())

 return ret



def macro_top(code):

 return """


# ABI_CHECK_LIBS_LINALG()
# -----------------------
#
# Looks for optimized versions of the linear algebra libraries.
#
AC_DEFUN([ABI_CHECK_LIBS_LINALG],
[dnl
 dnl Look until one library is found
 dnl
%s
]) # ABI_CHECK_LIBS_LINALG
""" % (code)



# ---------------------------------------------------------------------------- #

#
# Main program
#

# Initial setup
my_name    = "make-macros-linalg"
my_configs = ["config/specs/linalg.cf"]
my_output  = "config/m4/do-not-edit-linalg.m4"

# Check if we are in the top of the ABINIT source tree
if ( not os.path.exists("configure.ac") or
     not os.path.exists("src/main/abinit.F90") ):
 print "%s: You must be in the top of an ABINIT source tree." % my_name
 print "%s: Aborting now." % my_name
 sys.exit(1)

# Read config file(s)
for cnf in my_configs:
 if ( os.path.exists(cnf) ):
  execfile(cnf)
 else:
  print "%s: Could not find config file (%s)." % (my_name,cnf)
  print "%s: Aborting now." % my_name
  sys.exit(2)

# What time is it?
now = strftime("%Y/%m/%d %H:%M:%S +0000",gmtime())

# Start writing macro
m4 = file(my_output,"w")
m4.write(macro_header(my_name,now))

# Init
lib_tests     = ""
lib_tests_end = ""

# Loop over libraries
for lib in abinit_optlibs:
 lib_specs = abinit_optlibs[lib]

 # Add additional search paths
 if ( lib_specs[1] in abinit_optlib_paths ):
  lib_dirs_32 = " ".join(abinit_optlib_paths[lib_specs[1]])
 else:
  lib_dirs_32 = ""

 if ( lib_specs[2] in abinit_optlib_paths ):
  lib_dirs_64 = " ".join(abinit_optlib_paths[lib_specs[2]])
 else:
  lib_dirs_64 = ""

 if ( lib in abinit_optlib_paths ):
  lib_dirs_32 = " ".join(abinit_optlib_paths[lib])
  lib_dirs_64 = " ".join(abinit_optlib_paths[lib])

 # Write macro
 m4.write(macro_lib(lib,lib_specs[1],lib_specs[2],lib_dirs_32,lib_dirs_64))

 # Prepare top macro
 if ( lib_specs[0] == "" ):
  lib_tests_end += """
 dnl Look for %s
 if test "${abi_la_blas}" = "" -a "${abi_la_lapack}" = ""; then
  AC_MSG_NOTICE([starting search for the %s library])
  _ABI_CHECK_LIB_%s
  if test "${abi_la_blas}" = "%s" -a "${abi_la_lapack}" = "%s"; then
   AC_DEFINE([%s],1,[Define to 1 if you have the %s library])
   AC_MSG_NOTICE([found the %s library])
  fi
 fi
""" % (lib.upper(),lib.upper(),lib.upper(),lib,lib,lib_specs[3],lib.upper(),
       lib.upper())
 else:
  lib_tests += """
 dnl Look for %s
 if test "${fc_type}" = "%s"; then
  if test "${abi_la_blas}" = "" -a "${abi_la_lapack}" = ""; then
   AC_MSG_NOTICE([looking for the %s library])
   _ABI_CHECK_LIB_%s
   if test "${abi_la_blas}" = "%s" -a "${abi_la_lapack}" = "%s"; then
    AC_DEFINE([%s],1,[Define to 1 if you have the %s library])
    AC_MSG_NOTICE([found the %s library])
   fi
  fi
 fi
""" % (lib.upper(),lib_specs[0],lib.upper(),lib.upper(),lib,lib,lib_specs[3],
       lib.upper(),lib.upper())

# Finish
m4.write(macro_top(lib_tests+lib_tests_end))
m4.close()

tmp = commands.getoutput("./config/scripts/add-header-typed Autoconf %s" % (my_output))
if ( tmp != "" ):
 print tmp
