# CMake Preset
A Rust library for building C++ code using CMake Presets in Rust projects.
## Overview
This library provides a simple and type-safe way to integrate CMake builds into Rust projects. It uses the builder pattern with type states to ensure all required steps are performed in the correct order when building C++ code with CMake Presets.
## Features
- Type-safe builder pattern that enforces correct build steps order
- Automatic Cargo integration for linking C++ libraries
- Cross-platform support (Windows with MSVC, Linux with GCC)
- CMake Preset support for consistent builds
## Usage
Add this to your `Cargo.toml`:
```toml
[build-dependencies]
cmake-preset = { version = "0.1.0" }
```
Create a `build.rs` file in your project:
```rust
use cmake_preset::*;
fn main() {
if cfg!(target_os = "windows") {
CMakePresetBuilder::new()
.set_project_dir("cpp-lib")
.set_config_preset("msvc-x64-static-mt-rel")
.set_library_name("cpp-lib")
.config()
.build();
} else if cfg!(target_os = "linux") {
CMakePresetBuilder::new()
.set_project_dir("cpp-lib")
.set_config_preset("gcc-x64-static-rel")
.set_library_name("cpp-lib")
.config()
.build();
// Link libstdc++
println!("cargo:rustc-link-lib=static=stdc++");
}
}
```
Then you can link to and use your C++ functions in Rust:
```rust
unsafe extern "C" {
#[link_name = "Add"]
unsafe fn add(a: i32, b: i32) -> i32;
#[link_name = "Hello"]
unsafe fn hello();
}
fn main() {
println!("{}", unsafe { add(1, 2) });
unsafe { hello() };
}
```
## Builder Pattern and Type States
The library uses Rust's type system to enforce correct usage at compile time:
1. `StatusSetProjectDir` - Initial state, requires setting the project directory
2. `StatusSetConfigPreset` - Requires setting the CMake preset name
3. `StatusSetLibraryName` - Requires setting the static library name of generated
4. `StatusConfig` - Ready to run CMake configuration
5. `StatusBuild` - Ready to build the project
This ensures that all required steps are performed in the correct order.
## Example
See the `example` directory for a complete example of how to use this library to build and link a C++ library in a Rust project.