Skip to content

Commit b69e358

Browse files
committed
feat: production-ready improvements
1. Pin rocq-of-rust to specific commit with sha256: - Commit: 858907dbee116c51d7c6e87511bf5f92d6432ba4 - SHA256: 2fcfb09c3d14091f021b3aa7876ada55a29708c7f27f6646d3ebee162975bf61 2. Fail loudly when rocq-of-rust build fails: - New fail_on_error option (default: True) - Clear error message with troubleshooting steps - Set to False for placeholder mode 3. Comprehensive documentation: - Prerequisites section with Linux Rust requirements - API reference for all rules - Configuration options - Troubleshooting guide 4. Added integration test for advanced example
1 parent 622bbbd commit b69e358

4 files changed

Lines changed: 230 additions & 46 deletions

File tree

‎README.md‎

Lines changed: 166 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,42 @@ Bazel rules for Rocq/Coq theorem proving and Rust formal verification with herme
1010
- **Cross-Platform**: Linux, macOS (Intel & Apple Silicon)
1111
- **Bazel 8 bzlmod**: Modern module system support
1212

13-
## Prerequisites: Installing Nix
13+
## Prerequisites
1414

15-
**Nix is required** for the Rocq toolchain.
15+
### Nix (Required)
1616

17-
### macOS
17+
Nix is required for the Rocq toolchain.
1818

19+
**macOS:**
1920
```bash
2021
sh <(curl -L https://nixos.org/nix/install)
2122
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
2223
nix --version
2324
```
2425

25-
### Linux
26-
26+
**Linux:**
2727
```bash
2828
sh <(curl -L https://nixos.org/nix/install) --daemon
2929
nix --version
3030
```
3131

32+
### Rust Nightly (Linux only)
33+
34+
On Linux, rocq-of-rust requires Rust nightly with rustc internals:
35+
36+
```bash
37+
# Install nightly toolchain
38+
rustup toolchain install nightly-2024-12-01
39+
40+
# Add required components
41+
rustup component add rustc-dev rust-src --toolchain nightly-2024-12-01
42+
43+
# Set library path for LLVM (add to ~/.bashrc or ~/.zshrc)
44+
export LIBRARY_PATH="$(rustc +nightly-2024-12-01 --print sysroot)/lib:$LIBRARY_PATH"
45+
```
46+
47+
On macOS, Nix provides the complete Rust toolchain automatically.
48+
3249
## Quick Start
3350

3451
### 1. Add to MODULE.bazel
@@ -71,53 +88,174 @@ impl Point {
7188
### 3. Add BUILD.bazel
7289

7390
```starlark
74-
load("@rules_rocq_rust//coq_of_rust:defs.bzl", "coq_of_rust_library")
91+
load("@rules_rocq_rust//coq_of_rust:defs.bzl", "rocq_rust_verified_library")
7592
load("@rules_rocq_rust//rocq:defs.bzl", "rocq_library")
7693

77-
# Translate Rust to Rocq
78-
coq_of_rust_library(
79-
name = "point_translated",
94+
# One-liner: translate and compile
95+
rocq_rust_verified_library(
96+
name = "point_verified",
8097
rust_sources = ["point.rs"],
81-
edition = "2021",
82-
)
83-
84-
# Compile translated Rocq code
85-
rocq_library(
86-
name = "point_compiled",
87-
srcs = [":point_translated"],
88-
deps = ["@rocq_of_rust_source//:rocq_of_rust_main"],
8998
extra_flags = ["-impredicative-set"],
9099
)
91100

92101
# Write proofs about the Rust code
93102
rocq_library(
94103
name = "point_proofs",
95104
srcs = ["point_proofs.v"],
96-
deps = [":point_compiled", "@rocq_of_rust_source//:rocq_of_rust_main"],
105+
deps = [":point_verified"],
97106
extra_flags = ["-impredicative-set"],
98107
)
99108
```
100109

101110
### 4. Build and verify
102111

103112
```bash
104-
# Translate and compile
105-
bazel build //:point_compiled
106-
107-
# Verify proofs
108113
bazel build //:point_proofs
109114
```
110115

116+
## API Reference
117+
118+
### rocq_library
119+
120+
Compiles Rocq `.v` files to `.vo`.
121+
122+
```starlark
123+
rocq_library(
124+
name = "my_proofs",
125+
srcs = ["proof.v"],
126+
deps = [":other_library"],
127+
include_path = "MyProject", # Logical path prefix
128+
extra_flags = ["-impredicative-set"],
129+
)
130+
```
131+
132+
| Attribute | Description |
133+
|-----------|-------------|
134+
| `srcs` | Rocq source files (`.v`) |
135+
| `deps` | Dependencies on other `rocq_library` targets |
136+
| `include_path` | Logical path for this library (default: package path) |
137+
| `extra_flags` | Extra flags passed to coqc |
138+
139+
### coq_of_rust_library
140+
141+
Translates Rust source files to Rocq.
142+
143+
```starlark
144+
coq_of_rust_library(
145+
name = "translated",
146+
rust_sources = ["lib.rs"],
147+
edition = "2021",
148+
)
149+
```
150+
151+
| Attribute | Description |
152+
|-----------|-------------|
153+
| `rust_sources` | Rust source files to translate |
154+
| `edition` | Rust edition (default: "2021") |
155+
156+
### rocq_rust_verified_library
157+
158+
Convenience macro: translates Rust to Rocq and compiles.
159+
160+
```starlark
161+
rocq_rust_verified_library(
162+
name = "verified",
163+
rust_sources = ["lib.rs"],
164+
edition = "2021",
165+
deps = [], # Additional Rocq dependencies
166+
extra_flags = ["-impredicative-set"],
167+
)
168+
```
169+
170+
### rocq_proof_test
171+
172+
Test rule that verifies proofs compile successfully.
173+
174+
```starlark
175+
rocq_proof_test(
176+
name = "proofs_test",
177+
srcs = ["proofs.v"],
178+
deps = [":proofs"],
179+
)
180+
```
181+
182+
## Configuration Options
183+
184+
### rocq_of_rust.toolchain()
185+
186+
```starlark
187+
rocq_of_rust.toolchain(
188+
use_real_library = True, # Use full RocqOfRust library (recommended)
189+
fail_on_error = True, # Fail if rocq-of-rust build fails (default: True)
190+
rust_nightly = "nightly-2024-12-01", # Rust nightly version
191+
)
192+
```
193+
194+
| Option | Description |
195+
|--------|-------------|
196+
| `use_real_library` | Use full RocqOfRust library with coqutil/hammer/smpl |
197+
| `fail_on_error` | Fail build if rocq-of-rust cannot be built |
198+
| `rust_nightly` | Rust nightly version for building rocq-of-rust |
199+
200+
## Troubleshooting
201+
202+
### "unable to find library -lLLVM-19-rust-*" (Linux)
203+
204+
The Rust nightly compiler bundles its own LLVM. Set `LIBRARY_PATH`:
205+
206+
```bash
207+
export LIBRARY_PATH="$(rustc +nightly-2024-12-01 --print sysroot)/lib:$LIBRARY_PATH"
208+
```
209+
210+
### "cargo not found"
211+
212+
Install Rust via rustup:
213+
214+
```bash
215+
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
216+
```
217+
218+
### "rustc-dev component not found"
219+
220+
Install the required nightly components:
221+
222+
```bash
223+
rustup component add rustc-dev rust-src --toolchain nightly-2024-12-01
224+
```
225+
226+
### Build fails silently with placeholder
227+
228+
By default, builds fail loudly. If you see placeholder output, check the build logs for the actual error. To debug, run:
229+
230+
```bash
231+
bazel build @rocq_of_rust_source//:rocq_of_rust_main --verbose_failures
232+
```
233+
234+
### Proofs fail with "reference not found"
235+
236+
Ensure the translated code compiled successfully first:
237+
238+
```bash
239+
bazel build //:point_compiled
240+
```
241+
242+
Then check that your proof imports match the generated module names.
243+
111244
## Example
112245

113-
See `examples/rust_to_rocq/` for a complete working example with:
114-
- `point.rs` - Rust source code
115-
- Translated `point.v` (auto-generated)
116-
- `point_proofs.v` - Formal proofs about the Rust code
246+
See `examples/rust_to_rocq/` for a complete working example:
247+
248+
- `point.rs` - Rust source with Point and Rectangle structs
249+
- `advanced.rs` - Generics, traits, lifetimes, enums
250+
- `point_proofs.v` - Formal proofs about the translated code
117251

118252
```bash
119-
# Build the example
253+
# Build all examples
120254
bazel build //examples/rust_to_rocq:point_proofs
255+
bazel build //examples/rust_to_rocq:advanced_verified
256+
257+
# Run proof tests
258+
bazel test //examples/rust_to_rocq:point_proofs_test
121259
```
122260

123261
## How It Works
@@ -129,15 +267,13 @@ bazel build //examples/rust_to_rocq:point_proofs
129267

130268
## Toolchain Contents
131269

132-
The nixpkgs-based toolchain provides:
133-
134270
| Component | Description |
135271
|-----------|-------------|
136272
| Rocq 9.0 | Core theorem prover |
137273
| coqutil | Utility library |
138274
| Hammer | Automated proof tactics |
139275
| smpl | Simplification tactics |
140-
| rocq-of-rust | Rust-to-Rocq translator |
276+
| rocq-of-rust | Rust-to-Rocq translator (pinned version) |
141277

142278
## Supported Platforms
143279

‎coq_of_rust/extensions.bzl‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ _RocqOfRustToolchainTag = tag_class(
88
attrs = {
99
"commit": attr.string(
1010
doc = "Git commit hash or branch of rocq-of-rust",
11-
default = "main",
11+
default = "", # Empty means use pinned default
1212
),
1313
"sha256": attr.string(
1414
doc = "SHA256 of the source archive",
@@ -22,6 +22,10 @@ _RocqOfRustToolchainTag = tag_class(
2222
doc = "Use real RocqOfRust library (requires nixpkgs deps: coqutil, hammer, smpl)",
2323
default = False,
2424
),
25+
"fail_on_error": attr.bool(
26+
doc = "Fail build if rocq-of-rust cannot be built (recommended for production)",
27+
default = True,
28+
),
2529
},
2630
)
2731

@@ -37,24 +41,31 @@ def _rocq_of_rust_impl(module_ctx):
3741
# Use first configuration or defaults
3842
if configs:
3943
config = configs[0]
40-
commit = config.commit
44+
commit = config.commit if config.commit else "" # Empty uses pinned default
4145
sha256 = config.sha256
4246
rust_nightly = config.rust_nightly
4347
use_real_library = config.use_real_library
48+
fail_on_error = config.fail_on_error
4449
else:
45-
commit = "main"
50+
commit = "" # Uses pinned default in repository.bzl
4651
sha256 = ""
4752
rust_nightly = "nightly-2024-12-01"
4853
use_real_library = False
54+
fail_on_error = True
4955

5056
# Create source repository that downloads and builds rocq-of-rust
51-
rocq_of_rust_source(
52-
name = "rocq_of_rust_source",
53-
commit = commit,
54-
sha256 = sha256,
55-
rust_nightly = rust_nightly,
56-
use_real_library = use_real_library,
57-
)
57+
repo_kwargs = {
58+
"name": "rocq_of_rust_source",
59+
"rust_nightly": rust_nightly,
60+
"use_real_library": use_real_library,
61+
"fail_on_error": fail_on_error,
62+
}
63+
if commit:
64+
repo_kwargs["commit"] = commit
65+
if sha256:
66+
repo_kwargs["sha256"] = sha256
67+
68+
rocq_of_rust_source(**repo_kwargs)
5869

5970
# Create toolchain repository
6071
_create_toolchain_repo(name = "rocq_of_rust_toolchains")

0 commit comments

Comments
 (0)