Example #1
0
 def _transpile_example(self, main: str):
     transpile(self.cc_db,
               emit_build_files=False,
               extra_transpiler_args=[
                   '--emit-build-files', '--main', main,
                   '--overwrite-existing', '--output-dir', self.rust_src
               ])
def test_lua(args: argparse.Namespace) -> bool:
    """
    download lua, compile lua with bear to create
    a compiler command database, and use it to
    drive the transpiler.
    """

    if not os.path.isfile(os.path.join(c.DEPS_DIR, LUA_ARCHIVE)):
        with pb.local.cwd(c.DEPS_DIR):
            download_archive(LUA_URL, LUA_ARCHIVE)
    if not os.path.isdir(LUA_SRC):
        with pb.local.cwd(c.DEPS_DIR):
            invoke_quietly(TAR, "xf", LUA_ARCHIVE)

    # unconditionally compile lua since we don't know if
    # cc_db was generated from the environment we're in.
    build_dir = os.path.join(LUA_SRC, "build")
    rmtree(build_dir, ignore_errors=True)
    os.mkdir(build_dir)
    with pb.local.cwd(build_dir), pb.local.env(CC="clang"):
        invoke(CMAKE['-DCMAKE_EXPORT_COMPILE_COMMANDS=1', LUA_SRC])
        invoke(MAKE[JOBS])

    cc_db_file = os.path.join(LUA_SRC, "build", c.CC_DB_JSON)
    if not os.path.isfile(cc_db_file):
        die("missing " + cc_db_file, errno.ENOENT)

    return transpile(cc_db_file)
def test_json_c(args: argparse.Namespace) -> bool:
    if not os.path.isfile(os.path.join(c.DEPS_DIR, JSON_C_ARCHIVE)):
        with pb.local.cwd(c.DEPS_DIR):
            download_archive(JSON_C_URL, JSON_C_ARCHIVE)
            invoke_quietly(TAR, "xf", JSON_C_ARCHIVE)

    cc_db_file = os.path.join(JSON_C_SRC, c.CC_DB_JSON)
    # unconditionally compile json-c since we don't know if
    # cc_db was generated from the environment we're in.
    with pb.local.cwd(JSON_C_SRC), pb.local.env(CC="clang"):
        if os.path.isfile('Makefile'):
            invoke(MAKE['clean'])
        configure = pb.local.get("./configure")
        invoke(configure)
        invoke(BEAR[MAKE[JOBS]])

    if not os.path.isfile(cc_db_file):
        die("missing " + cc_db_file, errno.ENOENT)

    return transpile(cc_db_file)
def test_ruby(args: argparse.Namespace) -> bool:
    if on_mac():
        die("transpiling ruby on mac is not supported.")

    if not os.path.isfile(os.path.join(c.DEPS_DIR, RUBY_ARCHIVE)):
        with pb.local.cwd(c.DEPS_DIR):
            download_archive(RUBY_URL, RUBY_ARCHIVE)
            invoke_quietly(TAR, "xf", RUBY_ARCHIVE)

    cc_db_file = os.path.join(RUBY_SRC, c.CC_DB_JSON)

    # unconditionally compile ruby since we don't know if
    # cc_db was generated from the environment we're in.
    with pb.local.cwd(RUBY_SRC), pb.local.env(CC="clang",
                                              cflags="-w"):
        configure = pb.local.get("./configure")
        invoke(configure)
        invoke(BEAR[MAKE[JOBS]])

    if not os.path.isfile(cc_db_file):
        die("missing " + cc_db_file, errno.ENOENT)

    return transpile(cc_db_file)
Example #5
0
    "testSchemas",
    "testThreads",
    "testAutomata",
]

if __name__ == "__main__":
    args = parser.parse_args()

    assert os.path.isfile(COMPILE_COMMANDS), "Could not find {}".format(
        COMPILE_COMMANDS)

    # Build the tests first
    print(Colors.OKBLUE + "Transpiling tests..." + Colors.NO_COLOR)
    transpile(COMPILE_COMMANDS,
              filter='^(test|xmllint|runtest)',
              emit_build_files=False,
              cross_checks=args.cross_checks,
              cross_check_config=[CROSS_CHECK_CONFIG_YAML])

    print(Colors.OKBLUE + "Transpiling rest of files..." + Colors.NO_COLOR)
    transpile(COMPILE_COMMANDS,
              emit_build_files=True,
              cross_checks=args.cross_checks,
              cross_check_config=[CROSS_CHECK_CONFIG_YAML])

    # Create rust/examples directory if it doesn't exist
    mkdir_args = ["-p", RUST_EXAMPLES_DIR]
    retcode, stdout, stderr = mkdir[mkdir_args].run()

    assert retcode != 1, "Could not make directory:\n{}".format(stderr)
Example #6
0

if __name__ == "__main__":
    setup_logging()
    args = parser.parse_args()

    # Add option to use the debug version of `c2rust`
    config.update_args(args)

    assert os.path.isfile(COMPILE_COMMANDS), "Could not find {}".format(
        COMPILE_COMMANDS)

    print(Colors.OKBLUE + "Transpiling..." + Colors.NO_COLOR)
    transpile(
        COMPILE_COMMANDS,
        emit_build_files=False,
        reorganize_definitions=False,  # TODO
        extra_transpiler_args=["--reduce-type-annotations"])

    # Create the src dir if it doesn't already exist
    mkdir["-p", RUST_SRC_DIR].run()

    # Move and rename TCC.rs to main.rs
    move(TCC_RS, MAIN_RS)

    plumbum_rs_glob = local.path(TCC_REPO) // "*.rs"

    # Move source files to src directory
    retcode, _, _ = move(plumbum_rs_glob, RUST_SRC_DIR)

    assert retcode != 1, "Could not move translated rs files:\n{}".format(
Example #7
0
 def transpile(self):
     with pb.local.cwd(self.repo_dir):
         transpile(self.cc_db,
                   emit_build_files=False,
                   extra_transpiler_args=self.transpiler_args)
Example #8
0
def transpile_file(dirname, output_c_name):
    """Translate the given C file to Rust."""

    compile_commands_name = create_compile_commands(dirname, output_c_name)
    common.transpile(compile_commands_name, emit_build_files=False)