from buildutils import *

Import('env', 'build', 'install')

localenv = env.Clone()
linkflags = []

matlab_include = pjoin(localenv['matlab_path'], 'extern', 'include')

if localenv['OS'] == 'Windows':
    linklibs = list(env['cantera_shared_libs'])
    linklibs += ['libmx', 'libmex', 'libmat']
    if localenv['OS_BITS'] == 32:
        matlab_libs = pjoin(localenv['matlab_path'], 'extern',
                            'lib' ,'win32', 'microsoft')
        mexSuffix = '.mexw32'
    else:
        matlab_libs = pjoin(localenv['matlab_path'], 'extern',
                    'lib' ,'win64', 'microsoft')
        mexSuffix = '.mexw64'

    if localenv['CC'] == 'cl':
        linkflags.append('/export:mexFunction')
        machtype = 'X64' if localenv['OS_BITS'] == 64 else 'X86'
        linkflags.append('/MACHINE:' + machtype)
    elif localenv['CC'] == 'gcc':
        linkflags.append('-Wl,--export-all-symbols')
        linkflags.extend(['-static-libgcc', '-static-libstdc++'])

elif localenv['OS'] == 'Darwin':
    linklibs = list(env['cantera_libs'])
    linklibs += ['mx', 'mex', 'mat'] + env['LIBM']
    linkflags.extend(['-Wl,-exported_symbol,_mexFunction'])

    if localenv['OS_BITS'] == 64:
        matlab_libs = pjoin(localenv['matlab_path'], 'bin', 'maci64')
        mexSuffix = '.mexmaci64'
    else:
        matlab_libs = pjoin(localenv['matlab_path'], 'bin', 'macx86')
        mexSuffix = '.mexmaci'

elif os.name == 'posix':
    linklibs = list(env['cantera_libs'])
    linklibs += ['mx', 'mex', 'mat'] + env['LIBM']

    if localenv['OS_BITS'] == 64:
        matlab_libs = pjoin(localenv['matlab_path'], 'bin', 'glnxa64')
        mexSuffix = '.mexa64'
    else:
        matlab_libs = pjoin(localenv['matlab_path'], 'bin', 'glnx86')
        mexSuffix = '.mexglx'

    linkflags.extend(['-Wl,--no-undefined',
                      '-Wl,--version-script,src/matlab/mexFunction.map'])

localenv.Append(CPPPATH=['#include', '#src', matlab_include],
                CPPDEFINES=['MATLAB_MEX_FILE'],
                LIBPATH=[matlab_libs],
                LINKFLAGS=linkflags)

linklibs += localenv['sundials_libs']
linklibs += localenv['blas_lapack_libs']

ctmethods = build(localenv.SharedLibrary('#interfaces/matlab/toolbox/ctmethods',
                                         mglob(localenv, '.', 'cpp'),
                                         LIBPREFIX='',
                                         SHLIBPREFIX='',
                                         SHLIBSUFFIX=mexSuffix,
                                         LIBS=linklibs))

if localenv['OS'] in ('Windows', 'Darwin'):
    localenv.Depends(ctmethods, localenv['cantera_shlib'])
else:
    localenv.Depends(ctmethods, localenv['cantera_staticlib'])

env['matlab_extension'] = ctmethods

### Install the Matlab toolbox ###

# 'ctpath.m'
target = build(localenv.SubstFile('#interfaces/matlab/ctpath.m',
                                  '#interfaces/matlab/ctpath.m.in'))
install('$inst_matlab_dir', target)

# 'cantera_demos.m'
install('$inst_matlab_dir', '#samples/matlab/cantera_demos.m')
install(localenv.RecursiveInstall, '$inst_matlab_dir',
        '#interfaces/matlab/toolbox',
        exclude=['dll$', 'exp$', 'lib$', 'ilk$', 'manifest$'])
install(localenv.RecursiveInstall, pjoin('$inst_sampledir', 'matlab'), '#samples/matlab')

if os.name == 'nt':
    shlib = [f for f in localenv['cantera_shlib']
             if f.name.endswith('dll')]
    install('$inst_matlab_dir', shlib)
