def compile_file(f_root, srcdir, bindir, suffix='.c'):
    """Compile a single C or assembler file, with the given file root, "f_root",
       suffix "suffix", from the source directory, "srcdir", in to the bin
       directory, "bindir" using the general preprocessor and C compilation
       flags.

       Return True if the compilation success, False if it fails. Log
       everything in the event of failure

    """
    abs_src = os.path.join(f'{srcdir}', f'{f_root}{suffix}')
    abs_bin = os.path.join(f'{bindir}', f'{f_root}.o')

    # Construct the argument list
    arglist = [f'{gp["cc"]}']
    arglist.extend(gp['cflags'])
    arglist.extend(gp['cc_output_pattern'].format(f'{f_root}.o').split())
    arglist.extend(gp['cc_input_pattern'].format(abs_src).split())

    # Run the compilation, but only if the source file is newer than the
    # binary.
    succeeded = True
    res = None

    if not os.path.isfile(abs_bin) or (
            os.path.getmtime(abs_src) > os.path.getmtime(abs_bin)
    ):
        if gp['verbose']:
            log.debug(f'Compiling in directory {bindir}')
            log.debug(arglist_to_str(arglist))

        try:
            res = subprocess.run(
                arglist,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                cwd=bindir,
                timeout=gp['timeout'],
            )
            if res.returncode != 0:
                log.warning(
                    f'Warning: Compilation of {f_root}{suffix} from source ' +
                    f'directory {srcdir} to binary directory {bindir} failed'
                )
                succeeded = False
        except subprocess.TimeoutExpired:
            log.warning(
                f'Warning: Compilation of {f_root}{suffix} from source ' +
                f'directory {srcdir} to binary directory {bindir} timed out'
            )
            succeeded = False

    if not succeeded:
        log.debug('Command was:')
        log.debug(arglist_to_str(arglist))
        if res:
            log.debug(res.stdout.decode('utf-8'))
            log.debug(res.stderr.decode('utf-8'))

    return succeeded
Beispiel #2
0
def link_benchmark(bench):
    """Link the benchmark, "bench".

       Return True if link is successful, False otherwise."""
    abs_bd_b = os.path.join(gp['bd_benchdir'], bench)

    if not os.path.isdir(abs_bd_b):
        log.warning(
            'Warning: Unable to find build directory for benchmark {bench}'.
            format(bench=bench))
        return False

    # Use a flag to track warnings, but keep going through warnings.
    succeeded = True

    # Create the argument list
    binlist = create_link_binlist(abs_bd_b)
    if not binlist:
        succeeded = False
    arglist = create_link_arglist(bench, binlist)

    # Run the link
    if gp['verbose']:
        log.debug('Linking in directory {abs_bd_b}'.format(abs_bd_b=abs_bd_b))
        log.debug(arglist_to_str(arglist))

    try:
        res = subprocess.run(
            arglist,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            cwd=abs_bd_b,
            timeout=gp['timeout'],
        )
        if res.returncode != 0:
            log.warning('Warning: Link of benchmark "{bench}" failed'.format(
                bench=bench))
            succeeded = False

        log.debug(res.stdout.decode('utf-8'))
        log.debug(res.stderr.decode('utf-8'))

    except subprocess.TimeoutExpired:
        log.warning('Warning: link of benchmark "{bench}" timed out'.format(
            bench=bench))
        succeeded = False

    if not succeeded:
        log.debug('In directory "' + abs_bd_b + '"')
        log.debug('Command was:')
        log.debug(arglist_to_str(arglist))

    return succeeded