-
Notifications
You must be signed in to change notification settings - Fork 0
/
mex.py
77 lines (57 loc) · 2.26 KB
/
mex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# mex.py: Matlab extension builder
# Based on http://scons.org/wiki/MexBuilder by
# Joe VanAndel, [email protected], 2010/1/15
import os
import sys, traceback
from SCons import *
from SCons.Defaults import *
from subprocess import Popen,PIPE
def findMatlab(env, tool='mex'):
if env.get('MATLAB_PATH'):
return os.path.join(env['MATLAB_PATH'], tool)
extra_paths = [ '/usr/bin', '/usr/local/bin' ]
if env.has_key('OPT_PREFIX'):
extra_paths.append("%s/bin" % env['OPT_PREFIX'])
return env.WhereIs(tool, extra_paths)
def getMexPath(env):
mex = findMatlab(env)
if not mex:
mex = "mex"
return mex
def generate(env):
bld = SCons.Builder.Builder(action = '$MEX $SOURCE -o $TARGET $MATLAB_MEX_EXTRA $_CCCOMCOM $_LIBDIRFLAGS $_LIBFLAGS $LINKFLAGS',
emitter = "$SHLIBEMITTER",
suffix = '.$MEX_EXT',
target_scanner = SCons.Scanner.Prog.ProgramScanner(),
src_scanner = SCons.Scanner.C.CScanner(),
)
# bld = Builder(action = '$MEX $SOURCE -o $TARGET $MATLAB_MEX_EXTRA $_CCCOMCOM $_LIBDIRFLAGS $_LIBFLAGS $LINKFLAGS ')
# MexSuffixes = ['.c', '.cpp']
# mexscanner = SCons.Scanner.C.CScanner()
# for suffix in MexSuffixes:
# SCons.Tool.SourceFileScanner.add_scanner(suffix, mexscanner)
env['BUILDERS']['MEX'] = bld
env['MEX'] = findMatlab(env, 'mex')
mexext = findMatlab(env, 'mexext')
if not mexext:
mexext = 'mexext'
try:
cmd = [ mexext ]
# invoke matlab
p1 = Popen(cmd, stdout=PIPE)
env['MEX_EXT'] = p1.communicate()[0][:-1]
print "Matlab found, using mex extension: %s" % env['MEX_EXT']
except Exception, e:
if env.get('MATLAB_PATH'):
print "Could not automatically determine mex extension. " + str(e)
print '-'*60
traceback.print_exc(file=sys.stdout)
print '-'*60
else:
print "MEX wasn't found [set MATLAB_PATH to enable]."
env['MEX_EXT'] = ''
def exists(env):
if not findMatlab(env, 'mex'):
SCons.Warnings.warn(SCons.Warning, "Could not find mex program.")
return False
return True