Esempio n. 1
0
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')
Esempio n. 2
0
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')
Esempio n. 3
0
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')
Esempio n. 4
0
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')
Esempio n. 5
0
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')
Esempio n. 6
0
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')