def check_inputs(xcfile, cfg, lang): """Make an executable with all the inputs the xcpp file uses and make sure that all of them are valid. Return a dictionary with the info on all the inputs. """ fname = cfg['xcpp']['filename'] fname, fvar = gen_input_file(xcfile, fname) cmd = '%s%s ' % (gen_cmd(cfg, lang, 1), fname) trace(' - compiling %s ... ' % fname) out, err, _ = exec_cmd(cmd) if err != '': msg = "ERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n') trace(' - running executable ... ') out, err, _ = exec_cmd('./a.out') if err != '': msg = "\nERROR: The command `%s` returned the following " \ "error:\n%s" % ('./a.out', str(err)) error(msg) trace('done\n') var, defs = TextParser(out).parse_info() info = _make_map(var, fvar) os.remove('a.out') os.remove(fname) return info, defs
def _compile_cpp_file(in_fname, func, cfg): """Helper function """ out_fname = '%s/%s_mex' % (cfg['matlab']['pkg'], func.name) mexbin, _, _ = exec_cmd('mexext') mexbin = '%s.%s' % (out_fname, mexbin.strip()) force = cfg['matlab']['force'] epilog = cfg['matlab']['epilog'] verbose = cfg['matlab']['verbose'] make_new = True if isinstance(force, list) and (len(force) == 0 or func.num in force): pass elif force in ['True', 'true']: pass elif os.path.exists(mexbin): date_in = datetime.fromtimestamp(os.path.getmtime(in_fname)) date_out = datetime.fromtimestamp(os.path.getmtime(mexbin)) if date_in < date_out: make_new = False trace(' - skipping compilation\n') if make_new: cmd = gen_cmd(cfg, 'matlab', int(cfg['matlab']['debug'])) cmd = '%s%s -output %s %s' % (cmd, in_fname, out_fname, epilog) trace(' - compiling %s ... ' % in_fname) if verbose is True or verbose in ['true', 'True']: cmd += ' -v' trace('\n * command:\n %s\n * ' % str(cmd)) exec_cmd(cmd, True) else: _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def _compile(fname, mma, in_fname, cfg): """Helper function. """ cmd = gen_cmd(cfg, 'mathematica', int(cfg['mathematica']['debug'])) cmd = '%s%s -c -I%s -o %s.o' % (cmd, in_fname, mma, fname) trace(' - compiling %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def build_cpp(name, debug=None): """Compile a file and place it in bin. """ root = site.getuserbase() if debug is None: out = "-o %s/lib/excentury/bin/%s.run" % (root, name) else: out = "-DDEBUG=%s -o %s/lib/excentury/bin/%s.run%s" % (debug, root, name, debug) root = pth.abspath(pth.dirname(__file__) + "/cpp") cmd = "g++ -O3 %s %s/%s.cpp" % (out, root, name) out, err, _ = exec_cmd(cmd) eq_(err, "", "Build Error -->\n%s\n%s" % (cmd, err))
def write_tm_file(funcs, cfg, in_map): """Write the tm file and compile it with mprep. """ fname = cfg['xcpp']['filename'] contents = ':Evaluate: Begin["`Private`"]\n\n' for func in funcs: contents += TM_FUNC.format(name=m_name(func.name), args=_inputs(func.param, in_map, True), inputs=_inputs(func.param, in_map)) contents += ':Evaluate: End[]\n' in_fname = 'tmp-%s.tm' % fname trace('+ writing temporary file %s ... ' % in_fname) with open(in_fname, 'w') as tmp: tmp.write(contents) trace('done\n') mma = cfg['mathematica']['mlink'] # MPREP cmd = '%s/mprep %s -o %s' % (mma, in_fname, 'tmp-%s.cpp' % fname) trace(' - preparing %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n') # COMPILE_TM cmd = gen_cmd(cfg, 'mathematica', int(cfg['mathematica']['debug'])) in_fname = 'tmp-%s.cpp' % fname out_fname = 'tmp-%s.o' % fname cmd = '%s%s -c -I%s -o %s' % (cmd, in_fname, mma, out_fname) trace(' - compiling %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) # CLEAN UP os.remove('tmp-%s.cpp' % fname) os.remove('tmp-%s.tm' % fname) trace('done\n')
def _link(fname, mma, out_fname, cfg): """Helper function. """ cmd = gen_cmd(cfg, 'mathematica', int(cfg['mathematica']['debug'])) files = '%s.o tmp-%s.o' % (fname, fname) extras = '-lMLi3 -lstdc++ -framework Foundation' cmd = '%s%s -I%s -L%s %s -o %s' % (cmd, files, mma, mma, extras, out_fname) trace(' - preparing library ... ') _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) os.remove('%s.o' % fname) os.remove('tmp-%s.o' % fname) trace('done\n')
def run_cmd(cmd, exp_err, exp_out): """Run a command and compare the expected output and error.""" out, err, _ = exec_cmd(cmd) hline = "_" * 60 msg = ( "%s stderr -->\n%s\n%s\n%s\n\ %s expected stderr -->\n%s\n%s\n%s\n" % (cmd, hline, err, hline, cmd, hline, exp_err, hline) ) eq_(err, exp_err, msg) msg = ( "%s stdout -->\n%s\n%s\n%s\n\ %s expected stdout -->\n%s\n%s\n%s\n" % (cmd, hline, out, hline, cmd, hline, exp_out, hline) ) eq_(out, exp_out, msg)
def _compile_cpp_file(in_fname, cfg): """Helper function. """ out_fname = _get_exec_name(cfg) epilog = cfg['python']['epilog'] verbose = cfg['python']['verbose'] cmd = gen_cmd(cfg, 'python', int(cfg['python']['debug'])) cmd = '%s --shared -fPIC %s -o %s %s' % (cmd, in_fname, out_fname, epilog) trace(' - compiling %s ... ' % in_fname) if verbose is True or verbose in ['true', 'True']: trace('\n * command:\n %s\n * ' % str(cmd)) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def process_function(xcf, cfg, func): """Given a Function object from .xcpp it will create the contents of a valid cpp file that can be compiled for a valid executable. """ in_fname = cfg['xcpp']['filename'] epilog = func.epilog if epilog != '': epilog += '\n' contents = TEMPLATE.format(date=date(), pre_xc=xcf.pre_xc, preamble=xcf.preamble, help=_format_help(func, in_fname), param=_format_input(func.param), doc=func.doc, body=func.body, load=cfg['cpp']['load'].capitalize(), dump=cfg['cpp']['dump'].capitalize(), input=format_input(func.param), output=format_return(func.ret), out=func.out, funcpre=func.preamble, funcepi=epilog, epilog=xcf.epilog) in_fname = _write_file(contents, func, cfg) out_fname = _get_exec_name(func, cfg) force = cfg['cpp']['force'] make_new = True if isinstance(force, list) and (len(force) == 0 or func.num in force): pass elif force in ['True', 'true']: pass elif os.path.exists(out_fname): date_in = datetime.fromtimestamp(os.path.getmtime(in_fname)) date_out = datetime.fromtimestamp(os.path.getmtime(out_fname)) if date_in < date_out: make_new = False trace(' - skipping compilation\n') if make_new: cmd = gen_cmd(cfg, 'cpp', int(cfg['cpp']['debug'])) cmd = '%s%s -o %s' % (cmd, in_fname, out_fname) trace(' - compiling %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')