All Projects → immunant → C2rust

immunant / C2rust

Licence: other
Migrate C code to Rust

Programming Languages

rust
11053 projects
HTML
75241 projects
python
139335 projects - #7 most used programming language
C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language
lua
6591 projects

Projects that are alternatives of or similar to C2rust

Pseudo
transpile algorithms/libs to idiomatic JS, Go, C#, Ruby
Stars: ✭ 654 (-69.02%)
Mutual labels:  transpiler, translation
sqlglot
Python SQL Parser and Transpiler
Stars: ✭ 310 (-85.32%)
Mutual labels:  translation, transpiler
Translation Contracts
A set of translation abstractions extracted out of the Symfony components
Stars: ✭ 1,949 (-7.67%)
Mutual labels:  translation
How To Secure A Linux Server
An evolving how-to guide for securing a Linux server.
Stars: ✭ 11,939 (+465.56%)
Mutual labels:  security-hardening
Django Intro Zh
Django 官方文档的 intro 部分的中文翻译
Stars: ✭ 141 (-93.32%)
Mutual labels:  translation
Swiftrewriter
A Swift Package Manager console app and library to convert Objective-C code into Swift.
Stars: ✭ 140 (-93.37%)
Mutual labels:  transpiler
I18n Debug
Ever wondered which translations are being looked up by Rails, a gem, or simply your app? Wonder no more!
Stars: ✭ 143 (-93.23%)
Mutual labels:  translation
Pep8 Ja
PEP8 日本語版
Stars: ✭ 138 (-93.46%)
Mutual labels:  translation
Swiftui Tutorials
A code example and translation project of SwiftUI. / 一个 SwiftUI 的示例、翻译的教程项目。
Stars: ✭ 1,992 (-5.64%)
Mutual labels:  translation
Translateproject
Linux中国翻译项目
Stars: ✭ 1,847 (-12.51%)
Mutual labels:  translation
Matsuri translation
夏色祭工坊烤推机
Stars: ✭ 144 (-93.18%)
Mutual labels:  translation
Rapydscript Ng
A transpiler for a Python like language to JavaScript
Stars: ✭ 140 (-93.37%)
Mutual labels:  transpiler
Muchtrans
Yet another translation site built on Git
Stars: ✭ 140 (-93.37%)
Mutual labels:  translation
Onnxt5
Summarization, translation, sentiment-analysis, text-generation and more at blazing speed using a T5 version implemented in ONNX.
Stars: ✭ 143 (-93.23%)
Mutual labels:  translation
Rosid
Just-in-time development server and static site generator.
Stars: ✭ 139 (-93.42%)
Mutual labels:  transpiler
Appvm
Nix-based app VMs
Stars: ✭ 146 (-93.08%)
Mutual labels:  security-hardening
Mobile
📲 Kiwi.com mobile app written in React Native
Stars: ✭ 139 (-93.42%)
Mutual labels:  translation
Translateapp
📝 A translations app without interruptions, copy words and translate directly, show result by top view.
Stars: ✭ 1,722 (-18.43%)
Mutual labels:  translation
Icopy Site.github.io
icopy.site github mirror
Stars: ✭ 142 (-93.27%)
Mutual labels:  translation
Aesara
Aesara is a fork of the Theano library that is maintained by the PyMC developers. It was previously named Theano-PyMC.
Stars: ✭ 145 (-93.13%)
Mutual labels:  transpiler

GitHub Actions Status Azure Build Status Latest Version Rustc Version

C2Rust helps you migrate C99-compliant code to Rust. The translator (or transpiler) produces unsafe Rust code that closely mirrors the input C code. The primary goal of the translator is to preserve functionality; test suites should continue to pass after translation. Generating safe and idiomatic Rust code from C ultimately requires manual effort. However, we are building a scriptable refactoring tool that reduces the tedium of doing so. You can also cross-check the translated code against the original (tutorial).

Here's the big picture:

C2Rust overview

To learn more, check out our RustConf'18 talk on YouTube and try the C2Rust translator online at www.c2rust.com.

Documentation

To learn more about using and developing C2Rust, check out the manual. The manual is still a work-in-progress, so if you can't find something please let us know.

Installation

Prerequisites

C2Rust requires LLVM 7 or later with its corresponding clang compiler and libraries. Python 3.4 or later, CMake 3.4.3 or later, and openssl (1.0) are also required. These prerequisites may be installed with the following commands, depending on your platform:

  • Ubuntu 18.04, Debian 10, and later:

      apt install build-essential llvm clang libclang-dev cmake libssl-dev pkg-config python3
    
  • Arch Linux:

      pacman -S base-devel llvm clang cmake openssl python
    
  • NixOS / nix:

      nix-shell
    
  • OS X: XCode command-line tools and recent LLVM (we recommend the Homebrew version) are required.

      xcode-select --install
      brew install llvm python3 cmake openssl
    

Finally, installing the correct nightly Rust compiler with Rustup is required on all platforms. You will also need to add rustfmt and rustc-dev:

rustup install nightly-2019-12-05
rustup component add --toolchain nightly-2019-12-05 rustfmt rustc-dev

Installing from crates.io

cargo +nightly-2019-12-05 install c2rust

On OS X with Homebrew LLVM, you need to point the build system at the LLVM installation as follows:

LLVM_CONFIG_PATH=/usr/local/opt/llvm/bin/llvm-config cargo +nightly-2019-12-05 install c2rust

On Linux with Linuxbrew LLVM, you need to point the build system at the LLVM installation as follows:

LLVM_CONFIG_PATH=/home/linuxbrew/.linuxbrew/opt/llvm/bin/llvm-config cargo +nightly-2019-12-05 install c2rust    

Note: adjust LLVM_CONFIG_PATH accordingly if Linuxbrew was installed to your home directory.

On Gentoo, you need to point the build system to the location of libclang.so and llvm-config as follows:

LLVM_CONFIG_PATH=/path/to/llvm-config LIBCLANG_PATH=/path/to/libclang.so cargo +nightly-2019-12-05 install c2rust 

If you have trouble with building and installing, or want to build from the latest master, the developer docs provide more details on the build system.

Installing from Git

If you'd like to check our recently developed features or you urgently require a bugfixed version of c2rust you can install it directly from Git:

cargo +nightly-2019-12-05 install --git https://github.com/immunant/c2rust.git c2rust

Please note that the master branch is under constant development and you may expirience issues or crashes.

You should also set LLVM_CONFIG_PATH accordingly if required as described above.

Translating C to Rust

To translate C files specified in compile_commands.json (see below), run the c2rust tool with the transpile subcommand:

c2rust transpile compile_commands.json

(The c2rust refactor tool is also available for refactoring Rust code, see refactoring).

The translator requires the exact compiler commands used to build the C code. This information is provided via a compilation database file named compile_commands.json. (Read more about compilation databases here and here). Many build systems can automatically generate this file; we show a few examples below.

Once you have a compile_commands.json file describing the C build, translate the C code to Rust with the following command:

c2rust transpile path/to/compile_commands.json

To generate a Cargo.toml template for a Rust library, add the -e option:

c2rust transpile --emit-build-files path/to/compile_commands.json

To generate a Cargo.toml template for a Rust binary, do this:

c2rust transpile --binary myprog path/to/compile_commands.json

Where --binary myprog tells the transpiler to use the main method from myprog.rs as the entry point for a binary.

The translated Rust files will not depend directly on each other like normal Rust modules. They will export and import functions through the C API. These modules can be compiled together into a single static Rust library or binary.

There are several known limitations in this translator. The translator will emit a warning and attempt to skip function definitions that cannot be translated.

Generating compile_commands.json files

The compile_commands.json file can be automatically created using either cmake, intercept-build, or bear.

It may be a good idea to remove optimizations(-OX) from the compile commands file, as there are optimization builtins which we do not support translating.

... with cmake

When creating the initial build directory with cmake specify -DCMAKE_EXPORT_COMPILE_COMMANDS=1. This only works on projects configured to be built by cmake. This works on Linux and MacOS.

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ...

... with intercept-build

intercept-build (part of the scan-build tool) is recommended for non-cmake projects. intercept-build is bundled with clang under tools/scan-build-py but a standalone version can be easily installed via PIP with:

pip install scan-build

Usage:

intercept-build <build command>

You can also use intercept-build to generate a compilation database for compiling a single C file, for example:

intercept-build sh -c "cc program.c"

... with bear (linux only)

If you have bear installed, it can be used similarly to intercept-build:

bear <build command>

Contact

To report issues with translation or refactoring, please use our Issue Tracker.

To reach the development team, join our discord channel or email us at [email protected].

FAQ

I translated code on platform X but it didn't work correctly on platform Y

We run the C preprocessor before translation to Rust. This specializes the code to the host platform. For this reason, we do not support cross compiling translated code at the moment.

What platforms can C2Rust be run on?

The translator and refactoring tool support both macOS and Linux. Other features, such as cross checking the functionality between C and Rust code, are currently limited to Linux hosts.

Acknowledgements and Licensing

This material is available under the BSD-3 style license as found in the LICENSE file.

The C2Rust translator is inspired by Jamey Sharp's Corrode translator. We rely on Emscripten's Relooper algorithm to translate arbitrary C control flows.

This material is based upon work supported by the United States Air Force and DARPA under Contract No. FA8750-15-C-0124. Any opinions, findings and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the United States Air Force and DARPA. Distribution Statement A, “Approved for Public Release, Distribution Unlimited.”

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].