# Last Change: Sun Jan 04 07:00 PM 2009 J
# vim:syntax=python

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

from numscons import GetNumpyEnvironment
from numscons import CheckF77LAPACK, CheckF77Clib

from numscons import write_info

env = GetNumpyEnvironment(ARGUMENTS)
env.Tool('f2py')
env.Append(CPPPATH = ['Zeros'])
#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()])

#=======================
# Starting Configuration
#=======================
config = env.NumpyConfigure(custom_tests = {'CheckLAPACK' : CheckF77LAPACK})

#-----------------
# Checking Lapack
#-----------------
st = config.CheckLAPACK()
if not st:
    has_lapack = 0
else:
    has_lapack = 1

config.Finish()
write_info(env)

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

# minpack lib
minpack_src = [pjoin("minpack", s) for s in ["chkder.f", "dogleg.f",
"dpmpar.f", "enorm.f", "fdjac1.f", "fdjac2.f", "hybrd.f", "hybrd1.f",
"hybrj.f", "hybrj1.f", "lmder.f", "lmder1.f", "lmdif.f", "lmdif1.f", "lmpar.f",
"lmstr.f", "lmstr1.f", "qform.f", "qrfac.f", "qrsolv.f",
"r1mpyq.f", "r1updt.f", "rwupdt.f"]]
env.DistutilsStaticExtLibrary('minpack', source = minpack_src)

# rootfind lib
rootfind_src  = [pjoin("Zeros", s) for s in ["bisect.c", "brenth.c",
"brentq.c", "ridder.c"]]
env.DistutilsStaticExtLibrary('rootfind', source = rootfind_src)

env.AppendUnique(LIBS = ['minpack', 'rootfind'])
env.AppendUnique(LIBPATH = '.')

# _minpack pyextension
env.NumpyPythonExtension('_minpack', '_minpackmodule.c')

# _zeros pyextension
env.NumpyPythonExtension('_zeros', 'zeros.c')

# _lbfgsb pyextension
src = [pjoin('lbfgsb', i) for i in ['lbfgsb.pyf', 'lbfgsb.f', 
                                    'linpack.f', 'timer.f']]
env.NumpyPythonExtension('_lbfgsb', source = src)

# _cobyla pyextension
src = [pjoin('cobyla', i) for i in ['cobyla2.f', 'trstlp.f', 'cobyla.pyf']]
env.NumpyPythonExtension('_cobyla', source = src)

# _minpack2 pyextension
src = [pjoin('minpack2', i) for i in ['dcsrch.f', 'dcstep.f', 'minpack2.pyf']]
env.NumpyPythonExtension('minpack2', source = src)

# _nnls pyextension
src = [pjoin('nnls', i) for i in ['nnls.f', 'nnls.pyf']]
env.NumpyPythonExtension('_nnls', source = src)

# moduleTNC pyextension
env.NumpyPythonExtension('moduleTNC', 
                         source = [pjoin('tnc', i) for i in \
                                                   ['moduleTNC.c', 'tnc.c']])

# _slsqp pyextension
src = [pjoin('slsqp', i) for i in ['slsqp_optmz.f', 'slsqp.pyf']]
env.NumpyPythonExtension('_slsqp', source = src)
