1import os
2from os.path import join as pjoin
3from buildutils import *
4
5Import('env', 'build', 'install')
6
7localenv = env.Clone()
8linkflags = []
9
10matlab_include = pjoin(localenv['matlab_path'], 'extern', 'include')
11
12if localenv['OS'] == 'Windows':
13    linklibs = list(env['cantera_shared_libs'])
14    linklibs += ['libmx', 'libmex', 'libmat']
15    if localenv['OS_BITS'] == 32:
16        matlab_libs = pjoin(localenv['matlab_path'], 'extern',
17                            'lib' ,'win32', 'microsoft')
18        mexSuffix = '.mexw32'
19    else:
20        matlab_libs = pjoin(localenv['matlab_path'], 'extern',
21                    'lib' ,'win64', 'microsoft')
22        mexSuffix = '.mexw64'
23
24    if localenv['CC'] == 'cl':
25        linkflags.append('/export:mexFunction')
26        machtype = 'X64' if localenv['OS_BITS'] == 64 else 'X86'
27        linkflags.append('/MACHINE:' + machtype)
28    elif localenv['CC'] == 'gcc':
29        linkflags.append('-Wl,--export-all-symbols')
30
31elif localenv['OS'] == 'Darwin':
32    linklibs = list(env['cantera_libs'])
33    linklibs += ['mx', 'mex', 'mat'] + env['LIBM']
34    linkflags.extend(['-Wl,-exported_symbol,_mexFunction'])
35
36    if localenv['OS_BITS'] == 64:
37        matlab_libs = pjoin(localenv['matlab_path'], 'bin', 'maci64')
38        mexSuffix = '.mexmaci64'
39    else:
40        matlab_libs = pjoin(localenv['matlab_path'], 'bin', 'macx86')
41        mexSuffix = '.mexmaci'
42
43elif os.name == 'posix':
44    linklibs = list(env['cantera_libs'])
45    linklibs += ['octave', 'octinterp'] + env['LIBM']
46
47    if localenv['OS_BITS'] == 64:
48        matlab_libs = pjoin(localenv['matlab_path'], 'lib', 'octave', '6.4.0')
49        mexSuffix = '.mexa64'
50    else:
51        matlab_libs = pjoin(localenv['matlab_path'], 'lib', 'octave', '6.4.0')
52        mexSuffix = '.mexglx'
53
54    linkflags.extend(['-Wl,--no-undefined',
55                      '-Wl,--version-script,src/matlab/mexFunction.map',
56                      '-static-libstdc++'])
57
58localenv.Prepend(CPPPATH=['#include', '#src', matlab_include])
59localenv.Append(CPPDEFINES=['MATLAB_MEX_FILE'],
60                LIBPATH=[matlab_libs],
61                LINKFLAGS=linkflags)
62
63linklibs += localenv['sundials_libs']
64linklibs += localenv['blas_lapack_libs']
65
66ctmethods = build(localenv.SharedLibrary('#interfaces/matlab/toolbox/ctmethods',
67                                         multi_glob(localenv, '.', 'cpp'),
68                                         LIBPREFIX='',
69                                         SHLIBPREFIX='',
70                                         SHLIBSUFFIX=mexSuffix,
71                                         LIBS=linklibs))
72
73if localenv['OS'] in ('Windows'):
74    localenv.Depends(ctmethods, localenv['cantera_shlib'])
75else:
76    localenv.Depends(ctmethods, localenv['cantera_staticlib'])
77
78env['matlab_extension'] = ctmethods
79
80### Install the Matlab toolbox ###
81
82# 'ctpath.m'
83globalenv = env
84
85def copy_var(target, source, env):
86    if env['python_prefix'] == 'USER':
87        env['python_module_loc_sc'] = ''
88    else:
89        env['python_module_loc_sc'] = globalenv['python_module_loc']
90
91target = localenv.SubstFile('#interfaces/matlab/ctpath.m',
92                            '#interfaces/matlab/ctpath.m.in')
93localenv.AddPreAction(target, copy_var)
94localenv.Depends(target, env['install_python_action'])
95install('$inst_matlab_dir', target)
96
97# 'Contents.m'
98contents = localenv.SubstFile('#interfaces/matlab/Contents.m',
99                              '#interfaces/matlab/Contents.m.in')
100install('$inst_matlab_dir', contents)
101
102install(localenv.RecursiveInstall, '$inst_matlab_dir',
103        '#interfaces/matlab/toolbox',
104        exclude=['dll$', 'exp$', 'lib$', 'ilk$', 'manifest$'])
105install(localenv.RecursiveInstall, '$inst_sampledir/matlab', '#samples/matlab')
106
107if os.name == 'nt':
108    shlib = [f for f in localenv['cantera_shlib']
109             if f.name.endswith('dll')]
110    install('$inst_matlab_dir', shlib)
111