3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
7 # Hans-Martin von Gaudecker, 2012
12 Note that the script is run in the directory where it lives -- Matlab won't
13 allow it any other way.
15 For error-catching purposes, keep an own log-file that is destroyed if the
16 task finished without error. If not, it will show up as mscript_[index].log
17 in the bldnode directory.
21 ctx(features='run_m_script',
22 source='some_script.m',
23 target=['some_table.tex', 'some_figure.eps'],
28 from waflib import Task, TaskGen, Logs
30 MATLAB_COMMANDS = ['matlab']
33 ctx.find_program(MATLAB_COMMANDS, var='MATLABCMD', errmsg = """\n
34 No Matlab executable found!\n\n
35 If Matlab is needed:\n
36 1) Check the settings of your system path.
37 2) Note we are looking for Matlab executables called: %s
38 If yours has a different name, please report to hmgaudecker [at] gmail\n
40 Do not load the 'run_m_script' tool in the main wscript.\n\n""" % MATLAB_COMMANDS)
41 ctx.env.MATLABFLAGS = '-wait -nojvm -nosplash -minimize'
43 class run_m_script_base(Task.Task):
44 """Run a Matlab script."""
45 run_str = '"${MATLABCMD}" ${MATLABFLAGS} -logfile "${LOGFILEPATH}" -r "try, ${MSCRIPTTRUNK}, exit(0), catch err, disp(err.getReport()), exit(1), end"'
48 class run_m_script(run_m_script_base):
49 """Erase the Matlab overall log file if everything went okay, else raise an
50 error and print its 10 last lines.
53 ret = run_m_script_base.run(self)
54 logfile = self.env.LOGFILEPATH
57 if sys.version_info.major >= 3:
59 with open(logfile, mode=mode) as f:
60 tail = f.readlines()[-10:]
61 Logs.error("""Running Matlab on %r returned the error %r\n\nCheck the log file %s, last 10 lines\n\n%s\n\n\n""",
62 self.inputs[0], ret, logfile, '\n'.join(tail))
67 @TaskGen.feature('run_m_script')
68 @TaskGen.before_method('process_source')
69 def apply_run_m_script(tg):
70 """Task generator customising the options etc. to call Matlab in batch
71 mode for running a m-script.
74 # Convert sources and targets to nodes
75 src_node = tg.path.find_resource(tg.source)
76 tgt_nodes = [tg.path.find_or_declare(t) for t in tg.to_list(tg.target)]
78 tsk = tg.create_task('run_m_script', src=src_node, tgt=tgt_nodes)
79 tsk.cwd = src_node.parent.abspath()
80 tsk.env.MSCRIPTTRUNK = os.path.splitext(src_node.name)[0]
81 tsk.env.LOGFILEPATH = os.path.join(tg.bld.bldnode.abspath(), '%s_%d.log' % (tsk.env.MSCRIPTTRUNK, tg.idx))
83 # dependencies (if the attribute 'deps' changes, trigger a recompilation)
84 for x in tg.to_list(getattr(tg, 'deps', [])):
85 node = tg.path.find_resource(x)
87 tg.bld.fatal('Could not find dependency %r for running %r' % (x, src_node.abspath()))
88 tsk.dep_nodes.append(node)
89 Logs.debug('deps: found dependencies %r for running %r', tsk.dep_nodes, src_node.abspath())
91 # Bypass the execution of process_source by setting the source to an empty list