https://c2rust.com/manual/quickstart.html#translating-c-to-rust
https://c2rust.com/manual/docs/README-developers.html
以diffutils
为例,使用c2rust
工具,生成c
对应的rust
代码。
本文档分为两个部分:
一部分是:对c2rust
源码的操作,最终生成c2rust
二进制文件。
另一部分:是对diffutils
源码的操作,生成compile_commands.json
文件,并通过compile_commands.json
文件,生成最终的rust
文件。
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
git clone git@github.com:immunant/c2rust.git
./scripts/build_translator.py
运行完成后,会生成 target/release/c2rust
文件。
git clone git@github.com:Distrotech/diffutils.git
cd diffutils/
./configure
json
的工具:pip install scan-build
json
文件:intercept-build make
执行完成后,会在当前目录下生成compile_commands.json
文件。
这里因为编译命令就是make,所以使用intercept-build make
,如果是其他命令,需要修改为对应命令。(本人只测试过make
的情况)
在当前目录下执行如下命令,生成rust文件
~/c2rust/target/release/c2rust transpile compile_commands.json
其中,~/c2rust/target/release/c2rust
是上一步生成的c2rust
二进制文件。
最终效果如下:
通过c2rust转换出来的代码,直接使用是没有问题的,但是可读性很差,还是需要再手动调整下。