conan2-rs
Rust library for integrating Conan 2 into Cargo build scripts.
Usage
use ConanInstall;
use BuildPolicy;
// 1. Build conan install command
let command = builder?
.profile
.build
.path
.into_command;
// 2. Run conan install, parse JSON output
let root = command.run?;
// 3. Extract C++ build info for specific components
let cpp_info = root.get_cpp_info?;
// 4. Emit cargo link directives
for libdir in &cpp_info.lib_dirs
for lib in &cpp_info.libs
// 5. Generate Rust FFI bindings from C header via bindgen
let bindings = builder
.header
.clang_arg
.allowlist_function
.allowlist_type
.generate?;
bindings.write_to_file?;
API
| Module | Description |
|---|---|
command::install |
ConanInstall builder → run conan → parse JSON |
command::types |
BuildPolicy, ConanVerbosity etc. |
model |
ConanRoot, Graph, Recipe, CppInfo — conan JSON schema types |
model::visit |
get_cpp_info(), get_recipe_by_name() — graph traversal |
How it works
- Find conan — via
$CONANenv var orwhich conan - Run
conan install <conanfile> --format json --out-file ... - Parse JSON output into
ConanRoot(matches Conan graph JSON schema) - Visit the dependency graph to extract
CppInfoper component - Caller uses
include_dirs/lib_dirs/libsto configure the Rust build
Conan JSON schema (partial)
ConanRoot
└── graph: Graph
├── nodes: { id → Recipe }
│ └── cpp_info: { component → CppInfo }
│ ├── includedirs
│ ├── libdirs
│ └── libs
└── root: { context → node_id }