Example #1
0
def main():
    os.chdir(RFK_DIR)
    print('in %s' % RFK_DIR)

    # Remove object files that will confuse `transpile`
    rm['-f', 'src/robotfindskitten.o']()

    # Actually translate
    with open('compile_commands.json', 'r') as f:
        transpile.transpile_files(f, emit_build_files=False, verbose=True)

    # Move rust files into rust/src
    mkdir['-vp', 'rust/src']()
    mv['-v', local.path('src') // '*.rs', 'rust/src/']()

    # Refactor
    src_path = os.path.join(RFK_DIR, 'rust/src/robotfindskitten.rs')
    rf_hash = RefactorHash(idiomize, src_path)
    for refactor_str in REFACTORINGS:
        refactor_args = shlex.split(refactor_str)
        rf_hash.extend(refactor_str)

        cache_path = '%s.%s' % (src_path, rf_hash.hex)
        if os.path.isfile(cache_path):
            print('CACHED: %r' % (refactor_args, ))
            shutil.copy(cache_path, src_path)
        else:
            print('REFACTOR: %r' % (refactor_args, ))
            run_idiomize(refactor_args)
            shutil.copy(src_path, cache_path)
Example #2
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)
    with open(compile_commands_name) as compile_commands:
        transpile.transpile_files(
            compile_commands,
            None,  #filter
            [],  #extra impo args
            False,  #import_only
            False,  # verbose
            False,  # emit_build_files
        )
Example #3
0
def main():
    os.chdir(JSON_C_DIR)

    # Patch compile_commands to remove certain flags
    with open('compile_commands.json') as f:
        cc_json = json.load(f)

    for entry in cc_json:
        if 'arguments' not in entry:
            continue

        new_args = []
        for arg in entry['arguments']:
            if arg in ('-Werror', '-D_FORTIFY_SOURCE=2'):
                continue
            new_args.append(arg)

        entry['arguments'] = new_args

    with open('compile_commands.json', 'w') as f:
        json.dump(cc_json, f, indent=4)

    # Remove object files that will confuse `transpile`
    rm['-f', local.path('.') // '*.o', local.path('.libs') // '*.o']()

    # Actually translate
    with open('compile_commands.json', 'r') as f:
        transpile.transpile_files(f, emit_build_files=False, verbose=True)

    # Move rust files into rust/src
    mkdir['-vp', 'rust/src']()
    mv['-v', local.path('.') // '*.rs', 'rust/src/']()

    # Apply some fixes

    # 1. json-c uses `///` comments on some local assignment expressions, which
    # turn into `///` doc comments in the translated Rust.  But Rust doesn't
    # allow doc comments in those places.  We just globally replace `///` with
    # `//` for now.
    sed['-i', '-e', 's.///\+.//.g', local.path('rust/src') // '*.rs']()

    # 2. ast-importer omits _i8 annotation on the translations of certain
    # string literals in places where the type can't be inferred.
    sed['-i', '-e', r'/errno_str:/s/&\[\([0-9]\+\),/\&[\1i8,/',
        'rust/src/strerror_override.rs']()

    for refactor_args in REFACTORINGS:
        print('REFACTOR: %r' % (refactor_args, ))
        run_idiomize(refactor_args)
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)

    with open(cc_db_file) as cc_db:
        return transpile_files(cc_db, args.jobs, None, False, args.verbose)
Example #5
0
def main(xcheck: bool, snudown: str):
    setup_logging()

    # make sure the snudown submodule is checked out and up to date
    # update_or_init_submodule(snudown)

    # the macOS and Linux builds of the ast-importer alias each other
    if not is_elf_exe(AST_EXPO) and not on_mac():
        msg = "ast-importer was built for macOS;"
        msg += " please run build_translator.py and retry."
        die(msg)

    generate_html_entries_header(snudown)

    bldr = CompileCommandsBuilder()

    slugs = ["autolink", "buffer", "stack", "markdown"]
    ctmpl = "cc -o {snudown}/src/{slug}.c.o -c {snudown}/src/{slug}.c -Isrc -Ihtml -Wwrite-strings -D_FORTIFY_SOURCE=0 -DNDEBUG=1"

    for s in slugs:
        cmd = ctmpl.format(slug=s, snudown=snudown)
        file = os.path.join(snudown, "src", s + ".c")
        assert os.path.isfile(file), "No such file: " + file
        bldr.add_entry(snudown, cmd, file)

    cmds_json_path = bldr.write_result(os.path.curdir)
    with open(cmds_json_path, "r") as cmds_json:
        impo_args = ["--reloop-cfgs"]
        transpile.transpile_files(
            cmds_json,
            multiprocessing.cpu_count(),
            extra_impo_args=impo_args,
            emit_build_files=True,
            cross_checks=xcheck,
            cross_check_config=[os.path.join(snudown, "../snudown_rust.c2r")])

    with pb.local.cwd(os.path.join(snudown, "c2rust-build")):
        cargo = get_cmd_or_die("cargo")
        cargo("build")

    logging.info("success!")
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)

    with open(cc_db_file) as cc_db:
        transpile_files(cc_db, args.jobs, None, False, args.verbose)
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)

    with open(cc_db_file) as cc_db:
        return transpile_files(cc_db, args.jobs, None, False, args.verbose)
Example #8
0
        file.seek(0)
        file.write(text)
        file.truncate()


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

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

    with open(COMPILE_COMMANDS, 'r') as cc_json:
        transpile_files(cc_json,
                        filter=args.filter,
                        emit_build_files=False,
                        verbose=True)

    # Move and rename tmux.rs to main.rs
    move(TMUX_RS, MAIN_RS)

    plumbum_rs_glob = local.path(TMUX_REPO) // "*.rs"
    plumbum_compat_rs_glob = local.path(TMUX_COMPAT_DIR) // "*.rs"

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

    # Move compat files to src/compat directory
    retcode, _, _ = move(plumbum_compat_rs_glob, RUST_COMPAT_DIR)

    assert retcode != 1, "Could not move translated rs files:\n{}".format(
Example #9
0
C2RUST_DIR = config.ROOT_DIR
LIBXML2_REPO = os.path.join(C2RUST_DIR, "examples/libxml2/repo")
COMPILE_COMMANDS = os.path.join(LIBXML2_REPO, "compile_commands.json")
RUST_ROOT_DIR = os.path.join(LIBXML2_REPO, "rust")
RUST_EXAMPLES_DIR = os.path.join(RUST_ROOT_DIR, "examples")
RUST_SRC_DIR = os.path.join(RUST_ROOT_DIR, "src")

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

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

    with open(COMPILE_COMMANDS, 'r') as cc_json:
        transpile_files(cc_json, filter=args.filter, emit_build_files=False)

    # 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)

    # Move test files to examples directory (since they have their own main function)
    xmllint_rs = os.path.join(LIBXML2_REPO, "xmllint.rs")
    runtest_rs = os.path.join(LIBXML2_REPO, "runtest.rs")
    testapi_rs = os.path.join(LIBXML2_REPO, "testapi.rs")
    testsax_rs = os.path.join(LIBXML2_REPO, "testSAX.rs")
    testuri_rs = os.path.join(LIBXML2_REPO, "testURI.rs")
    testdict_rs = os.path.join(LIBXML2_REPO, "testdict.rs")
    testhtml_rs = os.path.join(LIBXML2_REPO, "testHTML.rs")
Example #10
0
        file.truncate()


if __name__ == "__main__":
    args = parser.parse_args()
    importer_args = [
        "--reduce-type-annotations",
    ]

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

    with open(COMPILE_COMMANDS, 'r') as cc_json:
        transpile_files(cc_json,
                        filter=lambda f: args.filter in f,
                        emit_build_files=False,
                        verbose=True,
                        extra_impo_args=importer_args,
                        reorganize_definitions=True)

    # Move and rename tmux.rs to main.rs
    move(TMUX_RS, MAIN_RS)

    plumbum_rs_glob = local.path(TMUX_REPO) // "*.rs"
    plumbum_compat_rs_glob = local.path(TMUX_COMPAT_DIR) // "*.rs"

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

    # Move compat files to src/compat directory
    retcode, _, _ = move(plumbum_compat_rs_glob, RUST_COMPAT_DIR)
Example #11
0
C2RUST_DIR = config.ROOT_DIR
LIBXML2_REPO = os.path.join(C2RUST_DIR, "examples/libxml2/repo")
COMPILE_COMMANDS = os.path.join(LIBXML2_REPO, "compile_commands.json")
RUST_ROOT_DIR = os.path.join(LIBXML2_REPO, "rust")
RUST_EXAMPLES_DIR = os.path.join(RUST_ROOT_DIR, "examples")
RUST_SRC_DIR = os.path.join(RUST_ROOT_DIR, "src")

if __name__ == "__main__":
    args = parser.parse_args()
    importer_args = ["--reloop-cfgs", "--translate-entry"]
    num_jobs = multiprocessing.cpu_count()

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

    with open(COMPILE_COMMANDS, 'r') as cc_json:
        transpile_files(cc_json, num_jobs, extra_impo_args=importer_args, filter=args.filter, emit_build_files=False)

    # 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)

    # Move test files to examples directory (since they have their own main function)
    xmllint_rs = os.path.join(LIBXML2_REPO, "xmllint.rs")
    runtest_rs = os.path.join(LIBXML2_REPO, "runtest.rs")
    testapi_rs = os.path.join(LIBXML2_REPO, "testapi.rs")
    testsax_rs = os.path.join(LIBXML2_REPO, "testSAX.rs")
    testuri_rs = os.path.join(LIBXML2_REPO, "testURI.rs")
    testdict_rs = os.path.join(LIBXML2_REPO, "testdict.rs")
    testhtml_rs = os.path.join(LIBXML2_REPO, "testHTML.rs")
Example #12
0
if __name__ == "__main__":
    args = parser.parse_args()
    num_jobs = multiprocessing.cpu_count()

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

    cross_check_configs = []
    if args.cross_checks:
        cross_check_configs.append(CROSS_CHECK_CONFIG_YAML)
    test_sources = set("%s.c" % test for test in TESTS)
    with open(COMPILE_COMMANDS, 'r') as cc_json:
        transpile_files(
            cc_json,
            filter=lambda f: f not in test_sources and args.filter in f,
            emit_build_files=False,
            emit_modules=True,
            cross_checks=args.cross_checks,
            cross_check_config=cross_check_configs)

    # Build the tests separately as full crates
    with open(COMPILE_COMMANDS, 'r') as cc_json:
        transpile_files(
            cc_json,
            filter=lambda f: f in test_sources and args.filter in f,
            emit_build_files=False,
            emit_modules=False,
            cross_checks=args.cross_checks,
            cross_check_config=cross_check_configs)

    # Create rust/examples directory if it doesn't exist