c2rust  by immunant

C to Rust transpiler for code migration

Created 8 years ago
4,747 stars

Top 10.3% on SourcePulse

GitHubView on GitHub
Project Summary

C2Rust is a transpiler designed to migrate C99-compliant codebases to Rust, preserving functional equivalence. It targets developers undertaking complex C-to-Rust rewrites, providing an initial unsafe Rust representation that serves as a foundation for subsequent manual or automated refinement into safe, idiomatic Rust code.

How It Works

The C2Rust toolchain employs a multi-stage process: c2rust transpile converts C sources into unsafe Rust, closely mirroring the original C structure. This is followed by c2rust refactor for deterministic code cleanup and deduplication, and optionally c2rust postprocess, which can leverage LLMs for more advanced idiomatic transformations. This approach prioritizes functional fidelity in the initial translation, deferring safety and idiomatic concerns to later, more manageable stages.

Quick Start & Requirements

  • Installation: Install via cargo install --locked c2rust. Development versions can be installed from Git.
  • Prerequisites: LLVM 15+ with clang and libraries, Python (uv), CMake 3.5+, and OpenSSL (1.0). Specific installation commands are provided for Ubuntu, Arch Linux, NixOS, and macOS.
  • Nightly Tools: Certain components like c2rust-refactor require specific nightly Rust compiler versions (e.g., nightly-2022-11-03) and are typically installed from Git.
  • Usage: The primary command is c2rust transpile compile_commands.json, which requires a compilation database generated by build systems like CMake, Meson, or tools like bear.
  • Docs: The most up-to-date manual is located in the ./manual/ directory of the repository. A live Compiler Explorer integration is also available.

Highlighted Details

  • Successfully used in projects like rav1d (AV1 decoder) and rexpat (XML parser), with some migrations achieving fully safe Rust.
  • Supports cross-architecture transpilation by specifying a target and sysroot.
  • Can generate Cargo.toml templates for Rust libraries or binaries, simplifying project setup.
  • The --reorganize-definitions flag integrates c2rust-refactor to deduplicate code and improve module imports.

Maintenance & Community

  • Issues can be reported via the project's Issue Tracker.
  • Community and development team contact is available via a Discord channel or email at c2rust@immunant.com.
  • The project has received support from the United States Air Force and DARPA.

Licensing & Compatibility

  • Licensed under a BSD-3 style license.
  • The permissive BSD license generally allows for commercial use and integration into closed-source projects.

Limitations & Caveats

The transpiled output is explicitly unsafe and unidiomatic, necessitating significant post-translation effort by developers. The LLM-powered postprocessing tool carries a risk of introducing errors, making a robust test suite essential. The translator may skip function definitions it cannot translate, emitting warnings. Cross-OS transpilation is noted as more challenging than cross-architecture.

Health Check
Last Commit

1 day ago

Responsiveness

Inactive

Pull Requests (30d)
38
Issues (30d)
6
Star History
28 stars in the last 30 days

Explore Similar Projects

Feedback? Help us improve.