# Last Change: Sat May 03 02:00 PM 2008 J
# vim:syntax=python

import os
from os.path import join as pjoin, splitext

from numscons import GetNumpyEnvironment
from numscons import CheckCBLAS, CheckF77BLAS,\
                     IsVeclib, IsAccelerate, \
                     IsATLAS, GetATLASVersion
from numscons import write_info

from scons_support import do_generate_fake_interface, generate_interface_emitter

env = GetNumpyEnvironment(ARGUMENTS)
env.Tool('f2py')

#if os.name == 'nt':
#    # NT needs the pythonlib to run any code importing Python.h, including
#    # simple code using only typedef and so on, so we need it for configuration
#    # checks
#    env.AppendUnique(LIBPATH = [get_pythonlib_dir()])

env['BUILDERS']['GenerateFakePyf'] = Builder(action = do_generate_fake_interface,
                                             emitter = generate_interface_emitter)

#=======================
# Starting Configuration
#=======================
config = env.NumpyConfigure(custom_tests = {'CheckCBLAS' : CheckCBLAS,
                                            'CheckBLAS' : CheckF77BLAS})

#--------------
# Checking Blas
#--------------
st = config.CheckBLAS(check_version = 1)
if not st:
    raise RuntimeError("no blas found, necessary for linalg module")
if IsATLAS(env, 'blas'):
    version = GetATLASVersion(env)
    env.Append(CPPDEFINES = [('ATLAS_INFO', '"\\"%s"\\"' % version)])
else:
    env.Append(CPPDEFINES = [('NO_ATLAS_INFO', 1)])

if config.CheckCBLAS():
    has_cblas = 1
else:
    has_cblas = 0

config.Finish()
write_info(env)

#==========
#  Build
#==========

# XXX: handle cblas wrapper for complex (check in numpy.scons or here ?)
env.AppendUnique(F2PYOPTIONS = '--quiet')

#------------
#   fblas
#------------
env.FromFTemplate('fblas.pyf', 'fblas.pyf.src')
source = ['fblas.pyf']
if IsVeclib(env, 'blas') or IsAccelerate(env, 'blas'):
    env.FromCTemplate('fblaswrap_veclib_c.c', 'fblaswrap_veclib_c.c.src')
    source.append('fblaswrap_veclib_c.c')
else:
    env.FromFTemplate('fblaswrap.f', 'fblaswrap.f.src')
    source.append('fblaswrap.f')
env.NumpyPythonExtension('fblas', source)

#------------
#   cblas
#------------
source = ['cblas.pyf']
if has_cblas:
    env.FromFTemplate('cblas.pyf', 'cblas.pyf.src')
else:
    print env.GenerateFakePyf('cblas', 'cblas.pyf.src')
env.NumpyPythonExtension('cblas', source)
