Skip to content

Commit ab7932f

Browse files
authored
Add documentation infrastructure (#301)
The documentation will sit in the `docs/` dir which contains a mdbook. The new `Deploy docs` workflow (inspired from [here](https://github.com/actions/starter-workflows/blob/main/pages/mdbook.yml)) builds the mdbook and, using Github Pages, hosts it at https://cpp2rust.github.io/cpp2rust/
1 parent 3586b65 commit ab7932f

10 files changed

Lines changed: 207 additions & 0 deletions

File tree

.github/workflows/docs.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Deploy docs
2+
3+
on:
4+
push:
5+
branches: [master]
6+
workflow_dispatch:
7+
8+
permissions:
9+
contents: read
10+
pages: write
11+
id-token: write
12+
13+
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- name: Install mdbook
19+
run: cargo install mdbook
20+
- name: Build book
21+
run: mdbook build docs
22+
- uses: actions/upload-pages-artifact@v3
23+
with:
24+
path: docs/book
25+
26+
deploy:
27+
needs: build
28+
runs-on: ubuntu-latest
29+
environment:
30+
name: github-pages
31+
url: ${{ steps.deployment.outputs.page_url }}
32+
steps:
33+
- id: deployment
34+
uses: actions/deploy-pages@v4

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

docs/book.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[book]
2+
title = "Cpp2Rust"
3+
description = "Developer documentation for Cpp2Rust, an automatic C++ to safe Rust translator."
4+
authors = ["The Cpp2Rust Authors"]
5+
language = "en"
6+
7+
[build]
8+
create-missing = false
9+
10+
[output.html]
11+
git-repository-url = "https://github.com/cpp2rust/cpp2rust"
12+
edit-url-template = "https://github.com/cpp2rust/cpp2rust/edit/master/docs/{path}"
13+
14+
[output.html.fold]
15+
enable = true
16+
level = 1

docs/src/SUMMARY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Summary
2+
3+
# The Project
4+
5+
* [Introduction](./project/introduction.md)
6+
* [Building](./project/building.md)
7+
* [Usage](./project/usage.md)
8+
* [Test Suite](./project/test-suite.md)
9+
10+
# Translation Rules
11+
12+
* [Overview](./rules/overview.md)
13+
14+
# Code Generation
15+
16+
* [Overview](./codegen/overview.md)

docs/src/codegen/overview.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Overview
2+
3+
This part of the book documents the internals of the code generator: how the
4+
clang AST is traversed and how Rust code is emitted.

docs/src/project/building.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Building
2+
3+
## Requirements
4+
5+
On Ubuntu, install the required dependencies with:
6+
7+
```bash
8+
sudo apt install libclang-22-dev clang++-22 ninja-build cmake
9+
pip install ruff==0.15.22
10+
```
11+
12+
## Build
13+
14+
```bash
15+
mkdir build
16+
cd build
17+
cmake -GNinja ..
18+
ninja
19+
ninja check
20+
```

docs/src/project/introduction.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Introduction
2+
3+
Cpp2Rust translates C++ to fully safe Rust automatically. It is a syntax-driven
4+
translator based on clang's AST.
5+
6+
Cpp2Rust's algorithm is described in the paper
7+
[Cpp2Rust: Automatic Translation of C++ to Safe Rust](https://web.ist.utl.pt/nuno.lopes/pubs/cpp2rust-pldi26.pdf)
8+
published at PLDI 2026.
9+
10+
## Overview
11+
12+
Cpp2Rust first parses the input C++ file(s) with clang and produces an AST.
13+
It then traverses the AST and emits Rust code as strings, inserting
14+
calls to the `libcc2rs` runtime library where needed (e.g., for raw pointer
15+
semantics).
16+
Finally, the Rust code is pretty-printed using `rustfmt` to a single `.rs` file.
17+
18+
By default the *reference counting model* is used, which produces fully safe
19+
Rust.
20+
A generator of unsafe Rust is also available through the `--model=unsafe`
21+
command line argument for debugging and performance comparisons.
22+
23+
## Runtime library (`libcc2rs`)
24+
25+
The generated code relies on a runtime library designed to simplify the
26+
translation process.
27+
C pointers are converted into the `Ptr<T>` type provided by `libcc2rs`.
28+
`Ptr<T>` models C pointer semantics, including null, arithmetic, and aliasing,
29+
while satisfying Rust's borrow checker through checked run-time operations.

docs/src/project/test-suite.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Test Suite
2+
3+
```bash
4+
# Run all tests
5+
ninja check
6+
7+
# Run only the unit tests
8+
ninja check-unit
9+
10+
# Run libcc2rs unit tests
11+
ninja check-libcc2rs
12+
13+
# Run libcc2rs-macros unit tests
14+
ninja check-libcc2rs-macros
15+
16+
# Regenerate expected output for unit tests after intentional changes
17+
REPLACE_EXPECTED=1 ninja check-unit
18+
```

docs/src/project/usage.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Usage
2+
3+
## Translate a single file
4+
5+
```bash
6+
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs
7+
```
8+
9+
By default, the reference counting model is used (fully safe output).
10+
To generate unsafe Rust instead:
11+
12+
```bash
13+
./build/cpp2rust/cpp2rust --file=<file>.cpp -o=<file>.rs --model=unsafe
14+
```
15+
16+
**Minimal example.** Given `hello.cpp`:
17+
18+
```cpp
19+
#include <cstdio>
20+
int main() {
21+
printf("hello world\n");
22+
return 0;
23+
}
24+
```
25+
26+
Running `./build/cpp2rust/cpp2rust --file=hello.cpp -o=hello.rs` produces:
27+
28+
```rust
29+
pub fn main() {
30+
std::process::exit(main_0());
31+
}
32+
fn main_0() -> i32 {
33+
println!("hello world");
34+
return 0;
35+
}
36+
```
37+
38+
Compile and run with:
39+
40+
```bash
41+
rustc hello.rs -L ../libcc2rs/target/debug
42+
./hello
43+
```
44+
45+
## Translate a whole program
46+
47+
First generate a
48+
[`compile_commands.json`](https://clang.llvm.org/docs/JSONCompilationDatabase.html)
49+
for your project. With CMake this is one extra flag:
50+
51+
```bash
52+
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
53+
```
54+
55+
Then run:
56+
57+
```bash
58+
./build/cpp2rust/cpp2rust --dir=<dir> -o <output>.rs
59+
```
60+
61+
`<dir>` must be the directory that contains `compile_commands.json`.

docs/src/rules/overview.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Overview
2+
3+
Translation rules describe how C++ library APIs are mapped to Rust.
4+
Each rule module lives in the `rules/` directory and pairs a C++ source file
5+
(`src.cpp`) with its Rust translation for each model (`tgt_refcount.rs` and
6+
`tgt_unsafe.rs`).
7+
8+
This part of the book explains how rules work and how to write new ones.

0 commit comments

Comments
 (0)