Exemplo n.º 1
0
    def _build_test_case(self):
        """Build an executable from the test source with the given --builder
        script and flags (--cflags, --ldflags) in the working directory.
        Or, if the --binary option has been given, copy the executable provided
        into the working directory and rename it to match the --builder output
        or skip if --vs-solution was passed on the command line.
        """

        if self.context.options.vs_solution:
            return

        options = self.context.options
        if options.binary:
            # Copy user's binary into the tmp working directory
            shutil.copy(options.binary, options.executable)
            builderIR = BuilderIR(name='binary',
                                  cflags=[options.binary],
                                  ldflags='')
        else:
            options = self.context.options
            compiler_options = [options.cflags for _ in options.source_files]
            linker_options = options.ldflags
            _, _, builderIR = run_external_build_script(
                self.context,
                script_path=self.build_script,
                source_files=options.source_files,
                compiler_options=compiler_options,
                linker_options=linker_options,
                executable_file=options.executable)
        return builderIR
Exemplo n.º 2
0
def run_external_build_script(context, script_path, source_files,
                              compiler_options, linker_options,
                              executable_file):
    """Build an executable using a builder script.

    The executable is saved to `context.working_directory.path`.

    Returns:
        ( stdout (str), stderr (str), builder (BuilderIR) )
    """

    builderIR = BuilderIR(
        name=context.options.builder,
        cflags=compiler_options,
        ldflags=linker_options,
    )
    assert len(source_files) == len(compiler_options), (source_files,
                                                        compiler_options)

    script_environ = _get_script_environment(source_files, compiler_options,
                                             linker_options, executable_file)
    env = dict(os.environ)
    env.update(script_environ)
    try:
        with Timer('running build script'):
            process = subprocess.Popen([script_path],
                                       cwd=context.working_directory.path,
                                       env=env,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
            out, err = process.communicate()
            returncode = process.returncode
        out = out.decode('utf-8')
        err = err.decode('utf-8')
        if returncode != 0:
            raise BuildScriptException(
                '{}: failed with returncode {}.\nstdout:\n{}\n\nstderr:\n{}\n'.
                format(script_path, returncode, out, err),
                script_error=err)
        return out, err, builderIR
    except OSError as e:
        raise BuildScriptException('{}: {}'.format(e.strerror, script_path))