import subprocess as subproc import os from pathlib import Path from mubench.lang import Language from mubench import CALLBACKS_DIR from mubench.exceptions import ExecutionFailure class Mu(Language): name = 'mu' src_ext = 'c' # one or a folder of C files calling API to build the bundle compiled = True default_exec = 'clang' # assume presence of clang @classmethod def compile(cls, src_file, target_dir=None, **compiler_conf): callback_dir = CALLBACKS_DIR / cls.name cmd = [] cmd.append(os.path.expandvars(compiler_conf.get('exec', cls.default_exec))) # flags flags = [] default_flags = ['-I%(callback_dir)s' % locals()] flags.extend(compiler_conf.get('flags', default_flags)) target_dir = target_dir if target_dir else src_file.parent target = target_dir / src_file.name[:-2] flags.extend(['-o', str(target)]) cmd.extend(flags) cmd.append(str(src_file)) # callback selected at compile time, initialised at runtime callback_file = callback_dir / compiler_conf['callback']['name'] cmd.append(str(callback_file)) cls.run_in_subproc(cmd) assert target.exists() return target @classmethod def run(cls, target, iterations, args, result_file, **runner_conf): cmd = [str(target)] # first argument: callback param cmd.append(runner_conf['callback']['param']) # second argument: iterations cmd.append(str(iterations)) # third argument: result file name cmd.append(str(result_file)) cmd.extend(map(str, args)) cls.run_in_subproc(cmd)