8 breaking releases

0.18.0 Jun 2, 2023
0.17.0 Feb 1, 2023
0.16.0 May 12, 2022
0.15.1 Oct 21, 2020
0.10.0 Apr 15, 2019

#864 in Development tools

Download history 30/week @ 2024-07-25 41/week @ 2024-08-01 36/week @ 2024-08-08 34/week @ 2024-08-15 27/week @ 2024-08-22 40/week @ 2024-08-29 44/week @ 2024-09-05 66/week @ 2024-09-12 48/week @ 2024-09-19 101/week @ 2024-09-26 58/week @ 2024-10-03 45/week @ 2024-10-10 52/week @ 2024-10-17 33/week @ 2024-10-24 46/week @ 2024-10-31 40/week @ 2024-11-07

185 downloads per month
Used in c2rust

BSD-3-Clause

1MB
24K SLoC

Docs

C2Rust Transpiler

Basic Usage

The transpiler module is invoked using the transpile sub-command of c2rust:

c2rust transpile [args] compile_commands.json [-- extra-clang-args]

The following arguments control the basic transpiler behavior:

  • --emit-modules - Emit each translated Rust file as a module (the default is to make each file its own crate).
  • --fail-on-error - Fail instead of warning if a source file cannot be fully translated.
  • --reduce-type-annotations - Do not emit explicit type annotations when unnecessary.
  • -f <regex>, --filter <regex> - Only translate files based on the regular expression used.

Creating cargo build files

The transpiler can create skeleton cargo build files for the translated Rust sources, controlled by the following options:

  • -e, --emit-build-files - Emit cargo build files to build the translated Rust code as a library. Build files are emitted in the directory specified by --output-dir, or if not specified, the directory containing compile_commands.json. This will not overwrite existing files, so remove these build files before re-creating build files. (implies --emit-modules)
  • -b <main_module>, --binary <main_module> - Emit cargo build files to build the translated Rust code as a binary. The main function must be found in the specified module (C source file) <main_module>. <main_module> should be the bare module name, not including the .rs extension. Build files are emitted in the directory specified by --output-dir, or if not specified, the directory containing compile_commands.json. This will not overwrite existing files, so remove this build file directory before re-creating build files. (implies --emit-build-files)

Cross-check instrumentation

The transpiler can instrument the transpiled Rust code for cross-checking. The following options control this instrumentation:

  • -x, --cross-checks - Add macros and build files for cross-checking.
  • --use-fakechecks - Link against the fakechecks library for cross-checking instead of using the default online checks.
  • -X <config>, --cross-check-config <config> - Use the given config file as the cross-checking config.

For Developers

The c2rust-transpile library uses the c2rust-ast-exporter library to translate C code to Rust. The ast-exporter library links against the native clang compiler front end to parse C code and exports the AST for use in the transpiler, which is then implemented purely in Rust.

Dependencies

~8–22MB
~265K SLoC