示例#1
0
def build_static_library(c_files: Iterable[CFile], output_path: str,
                         target: Optional[str]) -> Optional[CStaticLibrary]:
    current_path = os.getcwd()

    os.chdir(output_path)

    # create .o files
    args = ["-c", "-fPIC"]
    args.append(target_args(target))
    paths = [c_file.path for c_file in c_files]

    if len(paths) == 0:
        return
    else:
        args += paths

    logging.debug("compilation command:\n %s", str(clang[args]))
    retcode, stdout, stderr = clang[args].run(retcode=None)

    logging.debug("stdout:\n%s", stdout)

    if retcode != 0:
        raise NonZeroReturn(stderr)

    args = ["-rv", "libtest.a"]
    obj_files = []

    for c_file in c_files:
        extensionless_file_path, _ = os.path.splitext(c_file.path)
        extensionless_file_name = os.path.basename(extensionless_file_path)
        obj_file_path = os.path.join(output_path,
                                     extensionless_file_name + ".o")

        args.append(obj_file_path)
        obj_files.append(obj_file_path)

    logging.debug("combination command:\n %s", str(ar[args]))
    retcode, stdout, stderr = ar[args].run(retcode=None)

    logging.debug("stdout:\n%s", stdout)

    if retcode != 0:
        raise NonZeroReturn(stderr)

    os.chdir(current_path)

    return CStaticLibrary(output_path + "/libtest.a", "test", obj_files)
示例#2
0
    def translate(self, cc_db, extra_args: List[str] = []) -> RustFile:
        extensionless_file, _ = os.path.splitext(self.path)

        # help plumbum find rust
        ld_lib_path = get_rust_toolchain_libpath()
        if 'LD_LIBRARY_PATH' in pb.local.env:
            ld_lib_path += ':' + pb.local.env['LD_LIBRARY_PATH']

        # run the transpiler
        transpiler = get_cmd_or_die(c.TRANSPILER)

        args = [
            cc_db,
            "--prefix-function-names",
            "rust_",
            "--overwrite-existing",
        ]

        if self.disable_incremental_relooper:
            args.append("--no-incremental-relooper")
        if self.disallow_current_block:
            args.append("--fail-on-multiple")
        if self.translate_const_macros:
            args.append("--translate-const-macros")
        if self.reorganize_definitions:
            args.append("--reorganize-definitions")
        if self.emit_build_files:
            args.append("--emit-build-files")

        if self.logLevel == 'DEBUG':
            args.append("--log-level=debug")

        args.append("--")
        args.extend(extra_args)

        # Add -isysroot on MacOS to get SDK directory
        if on_mac():
            try:
                xcrun = pb.local["xcrun"]
                args.append("-isysroot" + xcrun("--show-sdk-path").strip())
            except pb.CommandNotFound:
                pass

        with pb.local.env(RUST_BACKTRACE='1', LD_LIBRARY_PATH=ld_lib_path):
            # log the command in a format that's easy to re-run
            translation_cmd = "LD_LIBRARY_PATH=" + ld_lib_path + " \\\n"
            translation_cmd += str(transpiler[args])
            logging.debug("translation command:\n %s", translation_cmd)
            retcode, stdout, stderr = (transpiler[args]).run(
                retcode=None)

            logging.debug("stdout:\n%s", stdout)
            logging.debug("stderr:\n%s", stderr)

        if retcode != 0:
            raise NonZeroReturn(stderr)

        return RustFile(extensionless_file + ".rs")
示例#3
0
def build_static_library(c_files: Iterable[CFile],
                         output_path: str) -> Optional[CStaticLibrary]:
    current_path = os.getcwd()

    os.chdir(output_path)

    # create .o files
    args = ["-c", "-fPIC", "-march=native"]

    args.extend(c_file.path for c_file in c_files)

    if len(args) == 2:
        return

    logging.debug("complication command:\n %s", str(clang[args]))
    retcode, stdout, stderr = clang[args].run(retcode=None)

    logging.debug("stdout:\n%s", stdout)

    if retcode != 0:
        raise NonZeroReturn(stderr)

    args = ["-rv", "libtest.a"]
    obj_files = []

    for c_file in c_files:
        extensionless_file_name, _ = os.path.splitext(c_file.path)
        args.append(extensionless_file_name + ".o")
        obj_files.append(extensionless_file_name + ".o")

    logging.debug("combination command:\n %s", str(ar[args]))
    retcode, stdout, stderr = ar[args].run(retcode=None)

    logging.debug("stdout:\n%s", stdout)

    if retcode != 0:
        raise NonZeroReturn(stderr)

    os.chdir(current_path)

    return CStaticLibrary(output_path + "/libtest.a", "test", obj_files)
示例#4
0
    def translate(self,
                  cc_db,
                  ld_lib_path,
                  extra_args: List[str] = []) -> RustFile:
        extensionless_file, _ = os.path.splitext(self.path)

        # run the transpiler
        transpiler = get_cmd_or_die(c.TRANSPILER)

        args = [
            cc_db,
            "--prefix-function-names",
            "rust_",
            "--overwrite-existing",
        ]

        if self.disable_incremental_relooper:
            args.append("--no-incremental-relooper")
        if self.disallow_current_block:
            args.append("--fail-on-multiple")
        if self.translate_const_macros:
            args.append("--translate-const-macros")
        if self.reorganize_definitions:
            args.append("--reorganize-definitions")
        if self.emit_build_files:
            args.append("--emit-build-files")

        if self.logLevel == 'DEBUG':
            args.append("--log-level=debug")

        args.append("--")
        args.extend(extra_args)

        with pb.local.env(RUST_BACKTRACE='1', LD_LIBRARY_PATH=ld_lib_path):
            # log the command in a format that's easy to re-run
            translation_cmd = "LD_LIBRARY_PATH=" + ld_lib_path + " \\\n"
            translation_cmd += str(transpiler[args])
            logging.debug("translation command:\n %s", translation_cmd)
            retcode, stdout, stderr = (transpiler[args]).run(retcode=None)

            logging.debug("stdout:\n%s", stdout)
            logging.debug("stderr:\n%s", stderr)

        if retcode != 0:
            raise NonZeroReturn(stderr)

        return RustFile(extensionless_file + ".rs")
示例#5
0
    def export(self, extra_args: List[str] = []) -> CborFile:
        ast_exporter = get_cmd_or_die(c.AST_EXPO)

        extra_args = ["-extra-arg={}".format(arg) for arg in extra_args]

        # run the exporter
        args = [self.path, *extra_args]

        # log the command in a format that's easy to re-run
        logging.debug("export command:\n %s", str(ast_exporter[args]))
        retcode, stdout, stderr = ast_exporter[args].run(retcode=None)

        logging.debug("stdout:\n%s", stdout)

        if retcode != 0:
            raise NonZeroReturn(stderr)

        return CborFile(self.path + ".cbor", self.enable_relooper,
                        self.disallow_current_block)
示例#6
0
    def compile(self,
                crate_type: CrateType,
                save_output: bool = False,
                extra_args: List[str] = []) -> Optional[LocalCommand]:
        current_dir, _ = os.path.split(self.path)
        extensionless_file, _ = os.path.splitext(self.path)

        # run rustc
        args = [
            "--crate-type={}".format(crate_type.value),
            "-L",
            current_dir,
            *extra_args,
        ]

        if save_output:
            args.append('-o')

            if crate_type == CrateType.Binary:
                args.append(extensionless_file)
            else:
                # REVIEW: Not sure if ext is correct
                args.append(extensionless_file + ".lib")

        args.append(self.path)

        # log the command in a format that's easy to re-run
        logging.debug("rustc compile command: %s", str(rustc[args]))

        retcode, stdout, stderr = rustc[args].run(retcode=None)

        logging.debug("stdout:\n%s", stdout)

        if retcode != 0:
            raise NonZeroReturn(stderr)

        if save_output:
            if crate_type == CrateType.Binary:
                return get_cmd_or_die(extensionless_file)
            # TODO: Support saving lib file

        return
示例#7
0
    def translate(self) -> RustFile:
        c_file_path, _ = os.path.splitext(self.path)
        extensionless_file, _ = os.path.splitext(c_file_path)
        rust_src = extensionless_file + ".rs"

        # help plumbum find rust
        ld_lib_path = get_rust_toolchain_libpath()
        if 'LD_LIBRARY_PATH' in pb.local.env:
            ld_lib_path += ':' + pb.local.env['LD_LIBRARY_PATH']

        # run the importer
        ast_importer = get_cmd_or_die(c.AST_IMPO)

        args = [
            self.path,
            "--prefix-function-names",
            "rust_",
        ]

        if self.enable_relooper:
            args.append("--reloop-cfgs")
            #  args.append("--use-c-loop-info")
            #  args.append("--use-c-multiple-info")
        if self.disallow_current_block:
            args.append("--fail-on-multiple")

        with pb.local.env(RUST_BACKTRACE='1', LD_LIBRARY_PATH=ld_lib_path):
            # log the command in a format that's easy to re-run
            translation_cmd = "LD_LIBRARY_PATH=" + ld_lib_path + " \\\n"
            translation_cmd += str(ast_importer[args] > rust_src)
            logging.debug("translation command:\n %s", translation_cmd)
            retcode, stdout, stderr = (ast_importer[args] > rust_src).run(
                retcode=None)

        logging.debug("stdout:\n%s", stdout)

        if retcode != 0:
            raise NonZeroReturn(stderr)

        return RustFile(extensionless_file + ".rs")
示例#8
0
    def export(self) -> CborFile:
        ast_exporter = get_cmd_or_die(c.AST_EXPO)

        # run the exporter
        args = [self.path]

        # NOTE: it doesn't seem necessary to specify system include
        # directories and in fact it may cause problems on macOS.
        # make sure we can locate system include files
        # sys_incl_dirs = get_system_include_dirs()
        # args += ["-extra-arg=-I" + i for i in sys_incl_dirs]

        # log the command in a format that's easy to re-run
        logging.debug("export command:\n %s", str(ast_exporter[args]))
        retcode, stdout, stderr = ast_exporter[args].run(retcode=None)

        logging.debug("stdout:\n%s", stdout)

        if retcode != 0:
            raise NonZeroReturn(stderr)

        return CborFile(self.path + ".cbor", self.enable_relooper,
                        self.disallow_current_block)